| | | 1 | | #define TI_DEBUG_PRINT |
| | | 2 | | //----------------------------------------------------------------------------- |
| | | 3 | | // |
| | | 4 | | // Copyright (C) Microsoft Corporation. All Rights Reserved. |
| | | 5 | | // Copyright by the contributors to the Dafny Project |
| | | 6 | | // SPDX-License-Identifier: MIT |
| | | 7 | | // |
| | | 8 | | //----------------------------------------------------------------------------- |
| | | 9 | | using System; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Numerics; |
| | | 13 | | using System.Diagnostics.Contracts; |
| | | 14 | | using System.IO; |
| | | 15 | | using System.Reflection; |
| | | 16 | | using JetBrains.Annotations; |
| | | 17 | | using Microsoft.BaseTypes; |
| | | 18 | | using Microsoft.Boogie; |
| | | 19 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 20 | | using Microsoft.Dafny.Plugins; |
| | | 21 | | using static Microsoft.Dafny.ErrorRegistry; |
| | | 22 | | |
| | | 23 | | namespace Microsoft.Dafny { |
| | | 24 | | public partial class Resolver { |
| | | 25 | | /// <summary> |
| | | 26 | | /// There are two rounds of name resolution + type inference. The "initialRound" parameter says which one to do. |
| | | 27 | | /// </summary> |
| | 2520 | 28 | | void ResolveNamesAndInferTypes(List<TopLevelDecl> declarations, bool initialRound) { |
| | 22680 | 29 | | foreach (TopLevelDecl topd in declarations) { |
| | 5040 | 30 | | Contract.Assert(topd != null); |
| | 5040 | 31 | | Contract.Assert(VisibleInScope(topd)); |
| | 5040 | 32 | | Contract.Assert(AllTypeConstraints.Count == 0); |
| | 5040 | 33 | | Contract.Assert(currentClass == null); |
| | | 34 | | |
| | 5040 | 35 | | allTypeParameters.PushMarker(); |
| | 5040 | 36 | | ResolveTypeParameters(topd.TypeArgs, !initialRound, topd); |
| | | 37 | | |
| | 7560 | 38 | | if (initialRound) { |
| | 2520 | 39 | | ResolveNamesAndInferTypesForOneDeclarationInitial(topd); |
| | 5040 | 40 | | } else { |
| | 2520 | 41 | | ResolveNamesAndInferTypesForOneDeclaration(topd); |
| | 2520 | 42 | | } |
| | | 43 | | |
| | 5040 | 44 | | allTypeParameters.PopMarker(); |
| | | 45 | | |
| | 5040 | 46 | | Contract.Assert(AllTypeConstraints.Count == 0); |
| | 5040 | 47 | | Contract.Assert(currentClass == null); |
| | 5040 | 48 | | } |
| | 2520 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Assumes type parameters of "topd" have already been pushed. |
| | | 53 | | /// </summary> |
| | 2520 | 54 | | void ResolveNamesAndInferTypesForOneDeclarationInitial(TopLevelDecl topd) { |
| | 2520 | 55 | | if (topd is NewtypeDecl newtypeDecl) { |
| | | 56 | | // this check can be done only after it has been determined that the redirected types do not involve cycles |
| | 0 | 57 | | AddXConstraint(newtypeDecl.tok, "NumericType", newtypeDecl.BaseType, "newtypes must be based on some numeric typ |
| | | 58 | | // type check the constraint, if any |
| | 0 | 59 | | if (newtypeDecl.Var != null) { |
| | 0 | 60 | | Contract.Assert(object.ReferenceEquals(newtypeDecl.Var.Type, newtypeDecl.BaseType)); // follows from NewtypeD |
| | 0 | 61 | | Contract.Assert(newtypeDecl.Constraint != null); // follows from NewtypeDecl invariant |
| | | 62 | | |
| | 0 | 63 | | scope.PushMarker(); |
| | 0 | 64 | | scope.AllowInstance = false; |
| | 0 | 65 | | var added = scope.Push(newtypeDecl.Var.Name, newtypeDecl.Var); |
| | 0 | 66 | | Contract.Assert(added == Scope<IVariable>.PushResult.Success); |
| | 0 | 67 | | ResolveExpression(newtypeDecl.Constraint, new ResolutionContext(new CodeContextWrapper(newtypeDecl, true), fal |
| | 0 | 68 | | Contract.Assert(newtypeDecl.Constraint.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 69 | | ConstrainTypeExprBool(newtypeDecl.Constraint, "newtype constraint must be of type bool (instead got {0})"); |
| | 0 | 70 | | scope.PopMarker(); |
| | 0 | 71 | | } |
| | 0 | 72 | | SolveAllTypeConstraints(); |
| | | 73 | | |
| | 3360 | 74 | | } else if (topd is SubsetTypeDecl subsetTypeDecl) { |
| | | 75 | | // type check the constraint |
| | 840 | 76 | | Contract.Assert(object.ReferenceEquals(subsetTypeDecl.Var.Type, subsetTypeDecl.Rhs)); // follows from SubsetType |
| | 840 | 77 | | Contract.Assert(subsetTypeDecl.Constraint != null); // follows from SubsetTypeDecl invariant |
| | 840 | 78 | | scope.PushMarker(); |
| | 840 | 79 | | scope.AllowInstance = false; |
| | 840 | 80 | | var added = scope.Push(subsetTypeDecl.Var.Name, subsetTypeDecl.Var); |
| | 840 | 81 | | Contract.Assert(added == Scope<IVariable>.PushResult.Success); |
| | 840 | 82 | | ResolveExpression(subsetTypeDecl.Constraint, new ResolutionContext(new CodeContextWrapper(subsetTypeDecl, true), |
| | 840 | 83 | | Contract.Assert(subsetTypeDecl.Constraint.Type != null); // follows from postcondition of ResolveExpression |
| | 840 | 84 | | ConstrainTypeExprBool(subsetTypeDecl.Constraint, "subset-type constraint must be of type bool (instead got {0})" |
| | 840 | 85 | | scope.PopMarker(); |
| | 840 | 86 | | SolveAllTypeConstraints(); |
| | 840 | 87 | | } |
| | | 88 | | |
| | 4200 | 89 | | if (topd is TopLevelDeclWithMembers cl) { |
| | 1680 | 90 | | ResolveClassMemberBodiesInitial(cl); |
| | 1680 | 91 | | } |
| | 2520 | 92 | | } |
| | | 93 | | |
| | 2520 | 94 | | void ResolveNamesAndInferTypesForOneDeclaration(TopLevelDecl topd) { |
| | 2520 | 95 | | if (topd is NewtypeDecl newtypeDecl) { |
| | 0 | 96 | | if (newtypeDecl.Witness != null) { |
| | 0 | 97 | | var codeContext = new CodeContextWrapper(newtypeDecl, newtypeDecl.WitnessKind == SubsetTypeDecl.WKind.Ghost); |
| | 0 | 98 | | scope.PushMarker(); |
| | 0 | 99 | | scope.AllowInstance = false; |
| | 0 | 100 | | ResolveExpression(newtypeDecl.Witness, new ResolutionContext(codeContext, false)); |
| | 0 | 101 | | scope.PopMarker(); |
| | 0 | 102 | | ConstrainSubtypeRelation(newtypeDecl.Var.Type, newtypeDecl.Witness.Type, newtypeDecl.Witness, "witness express |
| | 0 | 103 | | } |
| | 0 | 104 | | SolveAllTypeConstraints(); |
| | | 105 | | |
| | 3360 | 106 | | } else if (topd is SubsetTypeDecl subsetTypeDecl) { |
| | 840 | 107 | | if (subsetTypeDecl.Witness != null) { |
| | 0 | 108 | | var codeContext = new CodeContextWrapper(subsetTypeDecl, subsetTypeDecl.WitnessKind == SubsetTypeDecl.WKind.Gh |
| | 0 | 109 | | scope.PushMarker(); |
| | 0 | 110 | | scope.AllowInstance = false; |
| | 0 | 111 | | ResolveExpression(subsetTypeDecl.Witness, new ResolutionContext(codeContext, false)); |
| | 0 | 112 | | scope.PopMarker(); |
| | 0 | 113 | | ConstrainSubtypeRelation(subsetTypeDecl.Var.Type, subsetTypeDecl.Witness.Type, subsetTypeDecl.Witness, |
| | 0 | 114 | | "witness expression must have type '{0}' (got '{1}')", subsetTypeDecl.Var.Type, subsetTypeDecl.Witness.Type) |
| | 0 | 115 | | } |
| | 840 | 116 | | SolveAllTypeConstraints(); |
| | | 117 | | |
| | 2520 | 118 | | } else if (topd is IteratorDecl iteratorDecl) { |
| | 0 | 119 | | ResolveIterator(iteratorDecl); |
| | | 120 | | |
| | 1680 | 121 | | } else if (topd is DatatypeDecl dt) { |
| | | 122 | | // resolve any default parameters |
| | 0 | 123 | | foreach (var ctor in dt.Ctors) { |
| | 0 | 124 | | scope.PushMarker(); |
| | 0 | 125 | | scope.AllowInstance = false; |
| | 0 | 126 | | ctor.Formals.ForEach(p => scope.Push(p.Name, p)); |
| | 0 | 127 | | ResolveAttributes(ctor, new ResolutionContext(new NoContext(topd.EnclosingModuleDefinition), false), true); |
| | 0 | 128 | | ResolveParameterDefaultValues(ctor.Formals, ResolutionContext.FromCodeContext(dt)); |
| | 0 | 129 | | scope.PopMarker(); |
| | 0 | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | |
| | 4200 | 133 | | if (topd is TopLevelDeclWithMembers cl) { |
| | 1680 | 134 | | ResolveClassMemberBodies(cl); |
| | 1680 | 135 | | } |
| | | 136 | | |
| | | 137 | | // resolve attributes |
| | 2520 | 138 | | scope.PushMarker(); |
| | 2520 | 139 | | Contract.Assert(currentClass == null); |
| | 2520 | 140 | | scope.AllowInstance = false; |
| | 2520 | 141 | | if (topd is IteratorDecl iter) { |
| | 0 | 142 | | iter.Ins.ForEach(p => scope.Push(p.Name, p)); |
| | 0 | 143 | | } |
| | 2520 | 144 | | ResolveAttributes(topd, new ResolutionContext(new NoContext(topd.EnclosingModuleDefinition), false), true); |
| | 2520 | 145 | | scope.PopMarker(); |
| | 2520 | 146 | | } |
| | | 147 | | |
| | 33900 | 148 | | void EagerAddAssignableConstraint(IToken tok, Type lhs, Type rhs, string errMsgFormat) { |
| | | 149 | | Contract.Requires(tok != null); |
| | | 150 | | Contract.Requires(lhs != null); |
| | | 151 | | Contract.Requires(rhs != null); |
| | | 152 | | Contract.Requires(errMsgFormat != null); |
| | 33900 | 153 | | var lhsNormalized = lhs.Normalize(); |
| | 33900 | 154 | | var rhsNormalized = rhs.Normalize(); |
| | 45000 | 155 | | if (lhsNormalized is TypeProxy lhsProxy && !(rhsNormalized is TypeProxy)) { |
| | 11100 | 156 | | Contract.Assert(lhsProxy.T == null); // otherwise, lhs.Normalize() above would have kept on going |
| | 11100 | 157 | | AssignProxyAndHandleItsConstraints(lhsProxy, rhsNormalized, true); |
| | 33900 | 158 | | } else { |
| | 22800 | 159 | | AddAssignableConstraint(tok, lhs, rhs, errMsgFormat); |
| | 22800 | 160 | | } |
| | 33900 | 161 | | } |
| | 706250 | 162 | | public void AddAssignableConstraint(IToken tok, Type lhs, Type rhs, string errMsgFormat) { |
| | | 163 | | Contract.Requires(tok != null); |
| | | 164 | | Contract.Requires(lhs != null); |
| | | 165 | | Contract.Requires(rhs != null); |
| | | 166 | | Contract.Requires(errMsgFormat != null); |
| | 706250 | 167 | | AddXConstraint(tok, "Assignable", lhs, rhs, errMsgFormat); |
| | 706250 | 168 | | } |
| | 75640 | 169 | | private void AddXConstraint(IToken tok, string constraintName, Type type, string errMsgFormat) { |
| | | 170 | | Contract.Requires(tok != null); |
| | | 171 | | Contract.Requires(constraintName != null); |
| | | 172 | | Contract.Requires(type != null); |
| | | 173 | | Contract.Requires(errMsgFormat != null); |
| | 75640 | 174 | | var types = new Type[] { type }; |
| | 75640 | 175 | | AllXConstraints.Add(new XConstraint(tok, constraintName, types, new TypeConstraint.ErrorMsgWithToken(tok, errMsgFo |
| | 75640 | 176 | | } |
| | 322210 | 177 | | void AddAssignableConstraint(IToken tok, Type lhs, Type rhs, TypeConstraint.ErrorMsg errMsg) { |
| | | 178 | | Contract.Requires(tok != null); |
| | | 179 | | Contract.Requires(lhs != null); |
| | | 180 | | Contract.Requires(rhs != null); |
| | | 181 | | Contract.Requires(errMsg != null); |
| | 322210 | 182 | | AddXConstraint(tok, "Assignable", lhs, rhs, errMsg); |
| | 322210 | 183 | | } |
| | 420 | 184 | | private void AddXConstraint(IToken tok, string constraintName, Type type, TypeConstraint.ErrorMsg errMsg) { |
| | | 185 | | Contract.Requires(tok != null); |
| | | 186 | | Contract.Requires(constraintName != null); |
| | | 187 | | Contract.Requires(type != null); |
| | | 188 | | Contract.Requires(errMsg != null); |
| | 420 | 189 | | var types = new Type[] { type }; |
| | 420 | 190 | | AllXConstraints.Add(new XConstraint(tok, constraintName, types, errMsg)); |
| | 420 | 191 | | } |
| | 763810 | 192 | | private void AddXConstraint(IToken tok, string constraintName, Type type0, Type type1, string errMsgFormat) { |
| | | 193 | | Contract.Requires(tok != null); |
| | | 194 | | Contract.Requires(constraintName != null); |
| | | 195 | | Contract.Requires(type0 != null); |
| | | 196 | | Contract.Requires(type1 != null); |
| | | 197 | | Contract.Requires(errMsgFormat != null); |
| | 763810 | 198 | | var types = new Type[] { type0, type1 }; |
| | 763810 | 199 | | AllXConstraints.Add(new XConstraint(tok, constraintName, types, new TypeConstraint.ErrorMsgWithToken(tok, errMsgFo |
| | 763810 | 200 | | } |
| | 324670 | 201 | | private void AddXConstraint(IToken tok, string constraintName, Type type0, Type type1, TypeConstraint.ErrorMsg errMs |
| | | 202 | | Contract.Requires(tok != null); |
| | | 203 | | Contract.Requires(constraintName != null); |
| | | 204 | | Contract.Requires(type0 != null); |
| | | 205 | | Contract.Requires(type1 != null); |
| | | 206 | | Contract.Requires(errMsg != null); |
| | 324670 | 207 | | var types = new Type[] { type0, type1 }; |
| | 324670 | 208 | | AllXConstraints.Add(new XConstraint(tok, constraintName, types, errMsg)); |
| | 324670 | 209 | | } |
| | 1260 | 210 | | private void AddXConstraint(IToken tok, string constraintName, Type type, Expression expr0, Expression expr1, string |
| | | 211 | | Contract.Requires(tok != null); |
| | | 212 | | Contract.Requires(constraintName != null); |
| | | 213 | | Contract.Requires(type != null); |
| | | 214 | | Contract.Requires(expr0 != null); |
| | | 215 | | Contract.Requires(expr1 != null); |
| | | 216 | | Contract.Requires(errMsgFormat != null); |
| | 1260 | 217 | | var types = new Type[] { type }; |
| | 1260 | 218 | | var exprs = new Expression[] { expr0, expr1 }; |
| | 1260 | 219 | | AllXConstraints.Add(new XConstraintWithExprs(tok, constraintName, types, exprs, new TypeConstraint.ErrorMsgWithTok |
| | 1260 | 220 | | } |
| | | 221 | | |
| | | 222 | | [System.Diagnostics.Conditional("TI_DEBUG_PRINT")] |
| | 1833290 | 223 | | void PrintTypeConstraintState(int lbl) { |
| | 3666580 | 224 | | if (!Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 1833290 | 225 | | return; |
| | | 226 | | } |
| | 0 | 227 | | Options.OutputWriter.WriteLine("DEBUG: ---------- type constraints ---------- {0} {1}", lbl, lbl == 0 && currentMe |
| | 0 | 228 | | foreach (var constraint in AllTypeConstraints) { |
| | 0 | 229 | | var super = constraint.Super.Normalize(); |
| | 0 | 230 | | var sub = constraint.Sub.Normalize(); |
| | 0 | 231 | | Options.OutputWriter.WriteLine(" {0} :> {1}", super is IntVarietiesSupertype ? "int-like" : super is RealVari |
| | 0 | 232 | | } |
| | 0 | 233 | | foreach (var xc in AllXConstraints) { |
| | 0 | 234 | | Options.OutputWriter.WriteLine(" {0}", xc); |
| | 0 | 235 | | } |
| | 0 | 236 | | Options.OutputWriter.WriteLine(); |
| | 0 | 237 | | if (lbl % 2 == 1) { |
| | 0 | 238 | | Options.OutputWriter.WriteLine("DEBUG: --------------------------------------"); |
| | 0 | 239 | | } |
| | 1833290 | 240 | | } |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Attempts to fully solve all type constraints. |
| | | 244 | | /// Upon failure, reports errors. |
| | | 245 | | /// Clears all constraints. |
| | | 246 | | /// </summary> |
| | 72220 | 247 | | public void SolveAllTypeConstraints() { |
| | 72220 | 248 | | PrintTypeConstraintState(0); |
| | 72220 | 249 | | PartiallySolveTypeConstraints(true); |
| | 72220 | 250 | | PrintTypeConstraintState(1); |
| | 216660 | 251 | | foreach (var constraint in AllTypeConstraints) { |
| | 0 | 252 | | if (Type.IsSupertype(constraint.Super, constraint.Sub)) { |
| | | 253 | | // unexpected condition -- PartiallySolveTypeConstraints is supposed to have continued until no more sub-typin |
| | 0 | 254 | | Contract.Assume(false, string.Format("DEBUG: Unexpectedly satisfied supertype relation ({0} :> {1}) |||| ", co |
| | 0 | 255 | | } else { |
| | 0 | 256 | | constraint.FlagAsError(this); |
| | 0 | 257 | | } |
| | 0 | 258 | | } |
| | 216660 | 259 | | foreach (var xc in AllXConstraints) { |
| | 0 | 260 | | if (xc.Confirm(this, true, out var convertedIntoOtherTypeConstraints, out var moreXConstraints)) { |
| | | 261 | | // unexpected condition -- PartiallySolveTypeConstraints is supposed to have continued until no more XConstrai |
| | 0 | 262 | | Contract.Assume(false, string.Format("DEBUG: Unexpectedly confirmed XConstraint: {0} |||| ", xc)); |
| | 0 | 263 | | } else if (xc.CouldBeAnything()) { |
| | | 264 | | // suppress the error message; it will later be flagged as an underspecified type |
| | 0 | 265 | | } else { |
| | 0 | 266 | | xc.errorMsg.FlagAsError(this); |
| | 0 | 267 | | } |
| | 0 | 268 | | } |
| | 72220 | 269 | | TypeConstraint.ReportErrors(this, reporter); |
| | 72220 | 270 | | AllTypeConstraints.Clear(); |
| | 72220 | 271 | | AllXConstraints.Clear(); |
| | 72220 | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Adds type constraints for the expressions in the given attributes. |
| | | 276 | | /// |
| | | 277 | | /// If "solveConstraints" is "true", then the constraints are also solved. In this case, it is assumed on entry that |
| | | 278 | | /// prior type constraints. That is, the only type constraints being solved for are the ones in the given attributes |
| | | 279 | | /// </summary> |
| | 1821380 | 280 | | public void ResolveAttributes(IAttributeBearingDeclaration attributeHost, ResolutionContext resolutionContext, bool |
| | | 281 | | Contract.Requires(resolutionContext != null); |
| | | 282 | | Contract.Requires(attributeHost != null); |
| | | 283 | | |
| | 1821380 | 284 | | Contract.Assume(!solveConstraints || AllTypeConstraints.Count == 0); |
| | | 285 | | |
| | | 286 | | // order does not matter much for resolution, so resolve them in reverse order |
| | 5469180 | 287 | | foreach (var attr in attributeHost.Attributes.AsEnumerable()) { |
| | 1680 | 288 | | if (attr is UserSuppliedAttributes) { |
| | 0 | 289 | | var usa = (UserSuppliedAttributes)attr; |
| | 0 | 290 | | usa.Recognized = IsRecognizedAttribute(usa, attributeHost); |
| | 0 | 291 | | } |
| | 3360 | 292 | | if (attr.Args != null) { |
| | 7560 | 293 | | foreach (var arg in attr.Args) { |
| | 840 | 294 | | Contract.Assert(arg != null); |
| | 840 | 295 | | ResolveExpression(arg, resolutionContext); |
| | 840 | 296 | | } |
| | 1680 | 297 | | } |
| | 1680 | 298 | | } |
| | | 299 | | |
| | 1841850 | 300 | | if (solveConstraints) { |
| | 20470 | 301 | | SolveAllTypeConstraints(); |
| | 20470 | 302 | | } |
| | 1821380 | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// "IsTwoState" implies that "old" and "fresh" expressions are allowed. |
| | | 307 | | /// </summary> |
| | 2458360 | 308 | | public void ResolveExpression(Expression expr, ResolutionContext resolutionContext) { |
| | | 309 | | |
| | | 310 | | #if TEST_TYPE_SYNONYM_TRANSPARENCY |
| | | 311 | | ResolveExpressionX(expr, resolutionContext); |
| | | 312 | | // For testing purposes, change the type of "expr" to a type synonym (mwo-ha-ha-ha!) |
| | | 313 | | var t = expr.Type; |
| | | 314 | | Contract.Assert(t != null); |
| | | 315 | | var sd = new TypeSynonymDecl(expr.tok, "type#synonym#transparency#test", new TypeParameter.TypeParameterCharacteri |
| | | 316 | | new List<TypeParameter>(), resolutionContext.CodeContext.EnclosingModule, t, null); |
| | | 317 | | var ts = new UserDefinedType(expr.tok, "type#synonym#transparency#test", sd, new List<Type>(), null); |
| | | 318 | | expr.DebugTest_ChangeType(ts); |
| | | 319 | | } |
| | | 320 | | public void ResolveExpressionX(Expression expr, ResolutionContext resolutionContext) { |
| | | 321 | | #endif |
| | | 322 | | Contract.Requires(expr != null); |
| | | 323 | | Contract.Requires(resolutionContext != null); |
| | | 324 | | Contract.Ensures(expr.Type != null); |
| | 3469380 | 325 | | if (expr.Type != null) { |
| | | 326 | | // expression has already been resolved |
| | 1011020 | 327 | | return; |
| | | 328 | | } |
| | 1447340 | 329 | | DominatingStatementLabels.PushMarker(); |
| | | 330 | | |
| | | 331 | | // The following cases will resolve the subexpressions and will attempt to assign a type of expr. However, if err |
| | | 332 | | // and it cannot be determined what the type of expr is, then it is fine to leave expr.Type as null. In that case |
| | | 333 | | // of this method will assign proxy type to the expression, which reduces the number of error messages that are pr |
| | | 334 | | // while type checking the rest of the program. |
| | | 335 | | |
| | 1471670 | 336 | | if (expr is ParensExpression) { |
| | 24330 | 337 | | var e = (ParensExpression)expr; |
| | 24330 | 338 | | ResolveExpression(e.E, resolutionContext); |
| | 24330 | 339 | | var innerRange = e.E.RangeToken; |
| | 24330 | 340 | | e.ResolvedExpression = e.E; // Overwrites the range, which is not suitable for ParensExpressions |
| | 24330 | 341 | | e.E.RangeToken = innerRange; |
| | 24330 | 342 | | e.Type = e.E.Type; |
| | | 343 | | |
| | 1453580 | 344 | | } else if (expr is ChainingExpression) { |
| | 6240 | 345 | | var e = (ChainingExpression)expr; |
| | 6240 | 346 | | ResolveExpression(e.E, resolutionContext); |
| | 6240 | 347 | | e.ResolvedExpression = e.E; |
| | 6240 | 348 | | e.Type = e.E.Type; |
| | | 349 | | |
| | 1457090 | 350 | | } else if (expr is NegationExpression) { |
| | 34080 | 351 | | var e = (NegationExpression)expr; |
| | 34080 | 352 | | ResolveExpression(e.E, resolutionContext); |
| | 34080 | 353 | | e.Type = e.E.Type; |
| | 34080 | 354 | | AddXConstraint(e.E.tok, "NumericOrBitvector", e.E.Type, "type of unary - must be of a numeric or bitvector type |
| | | 355 | | // Note, e.ResolvedExpression will be filled in during CheckTypeInference, at which time e.Type has been determi |
| | | 356 | | |
| | 1694780 | 357 | | } else if (expr is LiteralExpr) { |
| | 278010 | 358 | | LiteralExpr e = (LiteralExpr)expr; |
| | | 359 | | |
| | 278010 | 360 | | if (e is StaticReceiverExpr) { |
| | 0 | 361 | | StaticReceiverExpr eStatic = (StaticReceiverExpr)e; |
| | 0 | 362 | | ResolveType(eStatic.tok, eStatic.UnresolvedType, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, nu |
| | 0 | 363 | | eStatic.Type = eStatic.UnresolvedType; |
| | 278010 | 364 | | } else { |
| | 278850 | 365 | | if (e.Value == null) { |
| | 840 | 366 | | e.Type = new InferredTypeProxy(); |
| | 840 | 367 | | AddXConstraint(e.tok, "IsNullableRefType", e.Type, "type of 'null' is a reference type, but it is used as {0 |
| | 359800 | 368 | | } else if (e.Value is BigInteger) { |
| | 81790 | 369 | | var proxy = new InferredTypeProxy(); |
| | 81790 | 370 | | e.Type = proxy; |
| | 81790 | 371 | | ConstrainSubtypeRelation(new IntVarietiesSupertype(), e.Type, e.tok, "integer literal used as if it had type |
| | 277170 | 372 | | } else if (e.Value is BaseTypes.BigDec) { |
| | 0 | 373 | | var proxy = new InferredTypeProxy(); |
| | 0 | 374 | | e.Type = proxy; |
| | 0 | 375 | | ConstrainSubtypeRelation(new RealVarietiesSupertype(), e.Type, e.tok, "type of real literal is used as {0}", |
| | 269410 | 376 | | } else if (e.Value is bool) { |
| | 74030 | 377 | | e.Type = Type.Bool; |
| | 260100 | 378 | | } else if (e is CharLiteralExpr) { |
| | 64720 | 379 | | e.Type = Type.Char; |
| | 177980 | 380 | | } else if (e is StringLiteralExpr) { |
| | 56630 | 381 | | e.Type = Type.String(); |
| | 56630 | 382 | | ResolveType(e.tok, e.Type, resolutionContext, ResolveTypeOptionEnum.DontInfer, null); |
| | 56630 | 383 | | } else { |
| | 0 | 384 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected literal type |
| | | 385 | | } |
| | 278010 | 386 | | } |
| | 1382690 | 387 | | } else if (expr is ThisExpr) { |
| | 0 | 388 | | if (!scope.AllowInstance) { |
| | 0 | 389 | | reporter.Error(MessageSource.Resolver, expr, "'this' is not allowed in a 'static' context"); |
| | 0 | 390 | | } |
| | 0 | 391 | | if (currentClass is ClassDecl cd && cd.IsDefaultClass) { |
| | | 392 | | // there's no type |
| | 0 | 393 | | } else { |
| | 0 | 394 | | if (currentClass == null) { |
| | 0 | 395 | | Contract.Assert(reporter.HasErrors); |
| | 0 | 396 | | } else { |
| | 0 | 397 | | expr.Type = GetThisType(expr.tok, currentClass); // do this regardless of scope.AllowInstance, for better e |
| | 0 | 398 | | } |
| | 0 | 399 | | } |
| | | 400 | | |
| | 1104680 | 401 | | } else if (expr is IdentifierExpr) { |
| | 0 | 402 | | var e = (IdentifierExpr)expr; |
| | 0 | 403 | | e.Var = scope.Find(e.Name); |
| | 0 | 404 | | if (e.Var != null) { |
| | 0 | 405 | | expr.Type = e.Var.Type; |
| | 0 | 406 | | } else { |
| | 0 | 407 | | reporter.Error(MessageSource.Resolver, expr, "Identifier does not denote a local variable, parameter, or bound |
| | 0 | 408 | | } |
| | | 409 | | |
| | 1247820 | 410 | | } else if (expr is DatatypeValue) { |
| | 143140 | 411 | | DatatypeValue dtv = (DatatypeValue)expr; |
| | 143140 | 412 | | if (!moduleInfo.TopLevels.TryGetValue(dtv.DatatypeName, out var d)) { |
| | 0 | 413 | | reporter.Error(MessageSource.Resolver, expr.tok, "Undeclared datatype: {0}", dtv.DatatypeName); |
| | 143140 | 414 | | } else if (d is AmbiguousTopLevelDecl) { |
| | 0 | 415 | | var ad = (AmbiguousTopLevelDecl)d; |
| | 0 | 416 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a type in one of the modu |
| | 143140 | 417 | | } else if (!(d is DatatypeDecl)) { |
| | 0 | 418 | | reporter.Error(MessageSource.Resolver, expr.tok, "Expected datatype: {0}", dtv.DatatypeName); |
| | 143140 | 419 | | } else { |
| | 143140 | 420 | | ResolveDatatypeValue(resolutionContext, dtv, (DatatypeDecl)d, null); |
| | 143140 | 421 | | } |
| | | 422 | | |
| | 1236390 | 423 | | } else if (expr is DisplayExpression) { |
| | 131710 | 424 | | DisplayExpression e = (DisplayExpression)expr; |
| | 131710 | 425 | | Type elementType = new InferredTypeProxy() { KeepConstraints = true }; |
| | 1374720 | 426 | | foreach (Expression ee in e.Elements) { |
| | 326530 | 427 | | ResolveExpression(ee, resolutionContext); |
| | 326530 | 428 | | Contract.Assert(ee.Type != null); // follows from postcondition of ResolveExpression |
| | 326530 | 429 | | ConstrainSubtypeRelation(elementType, ee.Type, ee.tok, "All elements of display must have some common supertyp |
| | 326530 | 430 | | } |
| | 172690 | 431 | | if (expr is SetDisplayExpr) { |
| | 40980 | 432 | | var se = (SetDisplayExpr)expr; |
| | 40980 | 433 | | expr.Type = new SetType(se.Finite, elementType); |
| | 173870 | 434 | | } else if (expr is MultiSetDisplayExpr) { |
| | 42160 | 435 | | expr.Type = new MultiSetType(elementType); |
| | 90730 | 436 | | } else { |
| | 48570 | 437 | | expr.Type = new SeqType(elementType); |
| | 48570 | 438 | | } |
| | 961540 | 439 | | } else if (expr is MapDisplayExpr) { |
| | 0 | 440 | | MapDisplayExpr e = (MapDisplayExpr)expr; |
| | 0 | 441 | | Type domainType = new InferredTypeProxy(); |
| | 0 | 442 | | Type rangeType = new InferredTypeProxy(); |
| | 0 | 443 | | foreach (ExpressionPair p in e.Elements) { |
| | 0 | 444 | | ResolveExpression(p.A, resolutionContext); |
| | 0 | 445 | | Contract.Assert(p.A.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 446 | | ConstrainSubtypeRelation(domainType, p.A.Type, p.A.tok, "All elements of display must have some common superty |
| | 0 | 447 | | ResolveExpression(p.B, resolutionContext); |
| | 0 | 448 | | Contract.Assert(p.B.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 449 | | ConstrainSubtypeRelation(rangeType, p.B.Type, p.B.tok, "All elements of display must have some common supertyp |
| | 0 | 450 | | } |
| | 0 | 451 | | expr.Type = new MapType(e.Finite, domainType, rangeType); |
| | 1512900 | 452 | | } else if (expr is NameSegment) { |
| | 683070 | 453 | | var e = (NameSegment)expr; |
| | 683070 | 454 | | ResolveNameSegment(e, true, null, resolutionContext, false); |
| | | 455 | | |
| | 683070 | 456 | | if (e.Type is Resolver_IdentifierExpr.ResolverType_Module) { |
| | 0 | 457 | | reporter.Error(MessageSource.Resolver, e.tok, "name of module ({0}) is used as a variable", e.Name); |
| | 0 | 458 | | e.ResetTypeAssignment(); // the rest of type checking assumes actual types |
| | 683070 | 459 | | } else if (e.Type is Resolver_IdentifierExpr.ResolverType_Type) { |
| | 0 | 460 | | reporter.Error(MessageSource.Resolver, e.tok, "name of type ({0}) is used as a variable", e.Name); |
| | 0 | 461 | | e.ResetTypeAssignment(); // the rest of type checking assumes actual types |
| | 0 | 462 | | } |
| | | 463 | | |
| | 836680 | 464 | | } else if (expr is ExprDotName) { |
| | 6850 | 465 | | var e = (ExprDotName)expr; |
| | 6850 | 466 | | ResolveDotSuffix(e, true, null, resolutionContext, false); |
| | 6850 | 467 | | if (e.Type is Resolver_IdentifierExpr.ResolverType_Module) { |
| | 0 | 468 | | reporter.Error(MessageSource.Resolver, e.tok, "name of module ({0}) is used as a variable", e.SuffixName); |
| | 0 | 469 | | e.ResetTypeAssignment(); // the rest of type checking assumes actual types |
| | 6850 | 470 | | } else if (e.Type is Resolver_IdentifierExpr.ResolverType_Type) { |
| | 0 | 471 | | reporter.Error(MessageSource.Resolver, e.tok, "name of type ({0}) is used as a variable", e.SuffixName); |
| | 0 | 472 | | e.ResetTypeAssignment(); // the rest of type checking assumes actual types |
| | 0 | 473 | | } |
| | | 474 | | |
| | 153410 | 475 | | } else if (expr is ApplySuffix) { |
| | 6650 | 476 | | var e = (ApplySuffix)expr; |
| | 6650 | 477 | | ResolveApplySuffix(e, resolutionContext, false); |
| | | 478 | | |
| | 139910 | 479 | | } else if (expr is MemberSelectExpr) { |
| | 0 | 480 | | var e = (MemberSelectExpr)expr; |
| | 0 | 481 | | ResolveExpression(e.Obj, resolutionContext); |
| | 0 | 482 | | Contract.Assert(e.Obj.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 483 | | var member = ResolveMember(expr.tok, e.Obj.Type, e.MemberName, out var tentativeReceiverType); |
| | 0 | 484 | | if (member == null) { |
| | | 485 | | // error has already been reported by ResolveMember |
| | 0 | 486 | | } else if (member is Function) { |
| | 0 | 487 | | var fn = member as Function; |
| | 0 | 488 | | e.Member = fn; |
| | 0 | 489 | | if (fn is TwoStateFunction && !resolutionContext.IsTwoState) { |
| | 0 | 490 | | reporter.Error(MessageSource.Resolver, e.tok, "a two-state function can be used only in a two-state context" |
| | 0 | 491 | | } |
| | | 492 | | // build the type substitution map |
| | 0 | 493 | | e.TypeApplication_AtEnclosingClass = tentativeReceiverType.TypeArgs; |
| | 0 | 494 | | e.TypeApplication_JustMember = new List<Type>(); |
| | | 495 | | Dictionary<TypeParameter, Type> subst; |
| | 0 | 496 | | var ctype = tentativeReceiverType as UserDefinedType; |
| | 0 | 497 | | if (ctype == null) { |
| | 0 | 498 | | subst = new Dictionary<TypeParameter, Type>(); |
| | 0 | 499 | | } else { |
| | 0 | 500 | | subst = TypeParameter.SubstitutionMap(ctype.ResolvedClass.TypeArgs, ctype.TypeArgs); |
| | 0 | 501 | | } |
| | 0 | 502 | | foreach (var tp in fn.TypeArgs) { |
| | 0 | 503 | | Type prox = new InferredTypeProxy(); |
| | 0 | 504 | | subst[tp] = prox; |
| | 0 | 505 | | e.TypeApplication_JustMember.Add(prox); |
| | 0 | 506 | | } |
| | 0 | 507 | | subst = BuildTypeArgumentSubstitute(subst); |
| | 0 | 508 | | e.Type = SelectAppropriateArrowTypeForFunction(fn, subst, builtIns); |
| | 0 | 509 | | } else if (member is Field) { |
| | 0 | 510 | | var field = (Field)member; |
| | 0 | 511 | | e.Member = field; |
| | 0 | 512 | | e.TypeApplication_AtEnclosingClass = tentativeReceiverType.TypeArgs; |
| | 0 | 513 | | e.TypeApplication_JustMember = new List<Type>(); |
| | 0 | 514 | | if (e.Obj is StaticReceiverExpr && !field.IsStatic) { |
| | 0 | 515 | | reporter.Error(MessageSource.Resolver, expr, "a field must be selected via an object, not just a class name" |
| | 0 | 516 | | } |
| | 0 | 517 | | var ctype = tentativeReceiverType as UserDefinedType; |
| | 0 | 518 | | if (ctype == null) { |
| | 0 | 519 | | e.Type = field.Type; |
| | 0 | 520 | | } else { |
| | 0 | 521 | | Contract.Assert(ctype.ResolvedClass != null); // follows from postcondition of ResolveMember |
| | | 522 | | // build the type substitution map |
| | 0 | 523 | | var subst = TypeParameter.SubstitutionMap(ctype.ResolvedClass.TypeArgs, ctype.TypeArgs); |
| | 0 | 524 | | e.Type = field.Type.Subst(subst); |
| | 0 | 525 | | } |
| | 0 | 526 | | } else { |
| | 0 | 527 | | reporter.Error(MessageSource.Resolver, expr, "member {0} in type {1} does not refer to a field or a function", |
| | 0 | 528 | | } |
| | | 529 | | |
| | 146290 | 530 | | } else if (expr is SeqSelectExpr) { |
| | 13030 | 531 | | SeqSelectExpr e = (SeqSelectExpr)expr; |
| | 13030 | 532 | | ResolveSeqSelectExpr(e, resolutionContext); |
| | | 533 | | |
| | 133260 | 534 | | } else if (expr is MultiSelectExpr) { |
| | 0 | 535 | | MultiSelectExpr e = (MultiSelectExpr)expr; |
| | | 536 | | |
| | 0 | 537 | | ResolveExpression(e.Array, resolutionContext); |
| | 0 | 538 | | Contract.Assert(e.Array.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 539 | | Contract.Assert(e.Array.Type.TypeArgs != null); // if it is null, should make a 1-element list with a Proxy |
| | 0 | 540 | | Type elementType = e.Array.Type.TypeArgs.Count > 0 ? |
| | 0 | 541 | | e.Array.Type.TypeArgs[0] : |
| | 0 | 542 | | new InferredTypeProxy(); |
| | 0 | 543 | | ConstrainSubtypeRelation(ResolvedArrayType(e.Array.tok, e.Indices.Count, elementType, resolutionContext, true), |
| | 0 | 544 | | "array selection requires an array{0} (got {1})", e.Indices.Count, e.Array.Type); |
| | 0 | 545 | | int i = 0; |
| | 0 | 546 | | foreach (Expression idx in e.Indices) { |
| | 0 | 547 | | Contract.Assert(idx != null); |
| | 0 | 548 | | ResolveExpression(idx, resolutionContext); |
| | 0 | 549 | | Contract.Assert(idx.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 550 | | ConstrainToIntegerType(idx, true, "array selection requires integer- or bitvector-based numeric indices (got { |
| | 0 | 551 | | i++; |
| | 0 | 552 | | } |
| | 0 | 553 | | e.Type = elementType; |
| | | 554 | | |
| | 121490 | 555 | | } else if (expr is SeqUpdateExpr) { |
| | 1260 | 556 | | SeqUpdateExpr e = (SeqUpdateExpr)expr; |
| | 1260 | 557 | | ResolveExpression(e.Seq, resolutionContext); |
| | 1260 | 558 | | Contract.Assert(e.Seq.Type != null); // follows from postcondition of ResolveExpression |
| | 1260 | 559 | | ResolveExpression(e.Index, resolutionContext); |
| | 1260 | 560 | | ResolveExpression(e.Value, resolutionContext); |
| | 1260 | 561 | | AddXConstraint(expr.tok, "SeqUpdatable", e.Seq.Type, e.Index, e.Value, "update requires a sequence, map, or mult |
| | 1260 | 562 | | expr.Type = new InferredTypeProxy(); // drop type constraints |
| | 1260 | 563 | | ConstrainSubtypeRelation( |
| | 1260 | 564 | | super: expr.Type, sub: e.Seq.Type, // expr.Type generalizes e.Seq.Type by dropping constraints |
| | 1260 | 565 | | exprForToken: expr, |
| | 1260 | 566 | | msg: "Update expression used with type '{0}'", e.Seq.Type); |
| | 120230 | 567 | | } else if (expr is DatatypeUpdateExpr) { |
| | 0 | 568 | | var e = (DatatypeUpdateExpr)expr; |
| | 0 | 569 | | ResolveExpression(e.Root, resolutionContext); |
| | 0 | 570 | | var ty = PartiallyResolveTypeForMemberSelection(expr.tok, e.Root.Type); |
| | 0 | 571 | | if (!ty.IsDatatype) { |
| | 0 | 572 | | reporter.Error(MessageSource.Resolver, expr, "datatype update expression requires a root expression of a datat |
| | 0 | 573 | | } else { |
| | 0 | 574 | | var (ghostLet, compiledLet) = ResolveDatatypeUpdate(expr.tok, e.Root, ty.AsDatatype, e.Updates, resolutionCont |
| | 0 | 575 | | out var members, out var legalSourceConstructors); |
| | 0 | 576 | | Contract.Assert((ghostLet == null) == (compiledLet == null)); |
| | 0 | 577 | | if (ghostLet != null) { |
| | 0 | 578 | | e.ResolvedExpression = ghostLet; // this might be replaced by e.ResolvedCompiledExpression in CheckIsCompila |
| | 0 | 579 | | e.ResolvedCompiledExpression = compiledLet; |
| | 0 | 580 | | e.Members = members; |
| | 0 | 581 | | e.LegalSourceConstructors = legalSourceConstructors; |
| | 0 | 582 | | expr.Type = ghostLet.Type; |
| | 0 | 583 | | } |
| | 0 | 584 | | } |
| | | 585 | | |
| | 118970 | 586 | | } else if (expr is FunctionCallExpr) { |
| | 0 | 587 | | var e = (FunctionCallExpr)expr; |
| | 0 | 588 | | ResolveFunctionCallExpr(e, resolutionContext); |
| | | 589 | | |
| | 118970 | 590 | | } else if (expr is ApplyExpr) { |
| | 0 | 591 | | var e = (ApplyExpr)expr; |
| | 0 | 592 | | ResolveExpression(e.Function, resolutionContext); |
| | 0 | 593 | | foreach (var arg in e.Args) { |
| | 0 | 594 | | ResolveExpression(arg, resolutionContext); |
| | 0 | 595 | | } |
| | | 596 | | |
| | | 597 | | // TODO: the following should be replaced by a type-class constraint that constrains the types of e.Function, e. |
| | 0 | 598 | | var fnType = e.Function.Type.AsArrowType; |
| | 0 | 599 | | if (fnType == null) { |
| | 0 | 600 | | reporter.Error(MessageSource.Resolver, e.tok, |
| | 0 | 601 | | "non-function expression (of type {0}) is called with parameters", e.Function.Type); |
| | 0 | 602 | | } else if (fnType.Arity != e.Args.Count) { |
| | 0 | 603 | | reporter.Error(MessageSource.Resolver, e.tok, |
| | 0 | 604 | | "wrong number of arguments to function application (function type '{0}' expects {1}, got {2})", fnType, |
| | 0 | 605 | | fnType.Arity, e.Args.Count); |
| | 0 | 606 | | } else { |
| | 0 | 607 | | for (var i = 0; i < fnType.Arity; i++) { |
| | 0 | 608 | | AddAssignableConstraint(e.Args[i].tok, fnType.Args[i], e.Args[i].Type, |
| | 0 | 609 | | "type mismatch for argument" + (fnType.Arity == 1 ? "" : " " + i) + " (function expects {0}, got {1})"); |
| | 0 | 610 | | } |
| | 0 | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | expr.Type = fnType == null ? new InferredTypeProxy() : fnType.Result; |
| | | 614 | | |
| | 118970 | 615 | | } else if (expr is SeqConstructionExpr) { |
| | 0 | 616 | | var e = (SeqConstructionExpr)expr; |
| | 0 | 617 | | var elementType = e.ExplicitElementType ?? new InferredTypeProxy(); |
| | 0 | 618 | | ResolveType(e.tok, elementType, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 619 | | ResolveExpression(e.N, resolutionContext); |
| | 0 | 620 | | ConstrainToIntegerType(e.N, false, "sequence construction must use an integer-based expression for the sequence |
| | 0 | 621 | | ResolveExpression(e.Initializer, resolutionContext); |
| | 0 | 622 | | var arrowType = new ArrowType(e.tok, builtIns.ArrowTypeDecls[1], new List<Type>() { builtIns.Nat() }, elementTyp |
| | 0 | 623 | | var hintString = " (perhaps write '_ =>' in front of the expression you gave in order to make it an arrow type)" |
| | 0 | 624 | | ConstrainSubtypeRelation(arrowType, e.Initializer.Type, e.Initializer, "sequence-construction initializer expres |
| | 0 | 625 | | arrowType, e.Initializer.Type, new LazyString_OnTypeEquals(elementType, e.Initializer.Type, hintString)); |
| | 0 | 626 | | expr.Type = new SeqType(elementType); |
| | | 627 | | |
| | 119340 | 628 | | } else if (expr is MultiSetFormingExpr) { |
| | 370 | 629 | | MultiSetFormingExpr e = (MultiSetFormingExpr)expr; |
| | 370 | 630 | | ResolveExpression(e.E, resolutionContext); |
| | 370 | 631 | | var elementType = new InferredTypeProxy(); |
| | 370 | 632 | | AddXConstraint(e.E.tok, "MultiSetConvertible", e.E.Type, elementType, "can only form a multiset from a seq or se |
| | 370 | 633 | | expr.Type = new MultiSetType(elementType); |
| | | 634 | | |
| | 118970 | 635 | | } else if (expr is OldExpr) { |
| | 0 | 636 | | var e = (OldExpr)expr; |
| | 0 | 637 | | e.AtLabel = ResolveDominatingLabelInExpr(expr.tok, e.At, "old", resolutionContext); |
| | 0 | 638 | | ResolveExpression(e.E, new ResolutionContext(resolutionContext.CodeContext, false) with { InOld = true }); |
| | 0 | 639 | | expr.Type = e.E.Type; |
| | | 640 | | |
| | 118600 | 641 | | } else if (expr is UnchangedExpr) { |
| | 0 | 642 | | var e = (UnchangedExpr)expr; |
| | 0 | 643 | | e.AtLabel = ResolveDominatingLabelInExpr(expr.tok, e.At, "unchanged", resolutionContext); |
| | 0 | 644 | | foreach (var fe in e.Frame) { |
| | 0 | 645 | | ResolveFrameExpression(fe, FrameExpressionUse.Unchanged, resolutionContext); |
| | 0 | 646 | | } |
| | 0 | 647 | | expr.Type = Type.Bool; |
| | | 648 | | |
| | 118600 | 649 | | } else if (expr is FreshExpr) { |
| | 0 | 650 | | var e = (FreshExpr)expr; |
| | 0 | 651 | | ResolveExpression(e.E, resolutionContext); |
| | 0 | 652 | | e.AtLabel = ResolveDominatingLabelInExpr(expr.tok, e.At, "fresh", resolutionContext); |
| | | 653 | | // the type of e.E must be either an object or a set/seq of objects |
| | 0 | 654 | | AddXConstraint(expr.tok, "Freshable", e.E.Type, "the argument of a fresh expression must denote an object or a s |
| | 0 | 655 | | expr.Type = Type.Bool; |
| | | 656 | | |
| | 127780 | 657 | | } else if (expr is UnaryOpExpr) { |
| | 9180 | 658 | | var e = (UnaryOpExpr)expr; |
| | 9180 | 659 | | ResolveExpression(e.E, resolutionContext); |
| | 9180 | 660 | | Contract.Assert(e.E.Type != null); // follows from postcondition of ResolveExpression |
| | 9180 | 661 | | switch (e.Op) { |
| | | 662 | | case UnaryOpExpr.Opcode.Not: |
| | 220 | 663 | | AddXConstraint(e.E.tok, "BooleanBits", e.E.Type, "logical/bitwise negation expects a boolean or bitvector ar |
| | 220 | 664 | | expr.Type = e.E.Type; |
| | 220 | 665 | | break; |
| | | 666 | | case UnaryOpExpr.Opcode.Cardinality: |
| | 8960 | 667 | | AddXConstraint(expr.tok, "Sizeable", e.E.Type, "size operator expects a collection argument (instead got {0} |
| | 8960 | 668 | | expr.Type = Type.Int; |
| | 8960 | 669 | | break; |
| | | 670 | | case UnaryOpExpr.Opcode.Allocated: |
| | | 671 | | // the argument is allowed to have any type at all |
| | 0 | 672 | | expr.Type = Type.Bool; |
| | 0 | 673 | | if (2 <= Options.Allocated && |
| | 0 | 674 | | ((resolutionContext.CodeContext is Function && !resolutionContext.InOld) || resolutionContext.CodeContext |
| | 0 | 675 | | var declKind = CodeContextWrapper.Unwrap(resolutionContext.CodeContext) is RedirectingTypeDecl redir ? red |
| | 0 | 676 | | reporter.Error(MessageSource.Resolver, expr, "a {0} definition is not allowed to depend on the set of allo |
| | 0 | 677 | | } |
| | 0 | 678 | | break; |
| | | 679 | | default: |
| | 0 | 680 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected unary operator |
| | | 681 | | } |
| | | 682 | | |
| | | 683 | | // We do not have enough information to compute `e.ResolvedOp` yet. |
| | | 684 | | // For binary operators the computation happens in `CheckTypeInference`. |
| | | 685 | | // For unary operators it happens lazily in the getter of `e.ResolvedOp`. |
| | 119540 | 686 | | } else if (expr is ConversionExpr) { |
| | 940 | 687 | | var e = (ConversionExpr)expr; |
| | 940 | 688 | | ResolveExpression(e.E, resolutionContext); |
| | 940 | 689 | | var prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 940 | 690 | | ResolveType(e.tok, e.ToType, resolutionContext, new ResolveTypeOption(ResolveTypeOptionEnum.InferTypeProxies), n |
| | 1880 | 691 | | if (reporter.Count(ErrorLevel.Error) == prevErrorCount) { |
| | 1880 | 692 | | if (e.ToType.IsNumericBased(Type.NumericPersuasion.Int)) { |
| | 940 | 693 | | AddXConstraint(expr.tok, "NumericOrBitvectorOrCharOrORDINAL", e.E.Type, "type conversion to an int-based typ |
| | 940 | 694 | | } else if (e.ToType.IsNumericBased(Type.NumericPersuasion.Real)) { |
| | 0 | 695 | | AddXConstraint(expr.tok, "NumericOrBitvectorOrCharOrORDINAL", e.E.Type, "type conversion to a real-based typ |
| | 0 | 696 | | } else if (e.ToType.IsBitVectorType) { |
| | 0 | 697 | | AddXConstraint(expr.tok, "NumericOrBitvectorOrCharOrORDINAL", e.E.Type, "type conversion to a bitvector-base |
| | 0 | 698 | | } else if (e.ToType.IsCharType) { |
| | 0 | 699 | | AddXConstraint(expr.tok, "NumericOrBitvectorOrCharOrORDINAL", e.E.Type, "type conversion to a char type is a |
| | 0 | 700 | | } else if (e.ToType.IsBigOrdinalType) { |
| | 0 | 701 | | AddXConstraint(expr.tok, "NumericOrBitvectorOrCharOrORDINAL", e.E.Type, "type conversion to an ORDINAL type |
| | 0 | 702 | | } else if (e.ToType.IsRefType) { |
| | 0 | 703 | | AddAssignableConstraint(expr.tok, e.ToType, e.E.Type, "type cast to reference type '{0}' must be from an exp |
| | 0 | 704 | | } else { |
| | 0 | 705 | | reporter.Error(MessageSource.Resolver, expr, "type conversions are not supported to this type (got {0})", e. |
| | 0 | 706 | | } |
| | 940 | 707 | | e.Type = e.ToType; |
| | 940 | 708 | | } else { |
| | 0 | 709 | | e.Type = new InferredTypeProxy(); |
| | 0 | 710 | | } |
| | | 711 | | |
| | 109420 | 712 | | } else if (expr is TypeTestExpr) { |
| | 0 | 713 | | var e = (TypeTestExpr)expr; |
| | 0 | 714 | | ResolveExpression(e.E, resolutionContext); |
| | 0 | 715 | | var prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 716 | | ResolveType(e.tok, e.ToType, resolutionContext, new ResolveTypeOption(ResolveTypeOptionEnum.InferTypeProxies), n |
| | 0 | 717 | | AddAssignableConstraint(expr.tok, e.ToType, e.E.Type, "type test for type '{0}' must be from an expression assig |
| | 0 | 718 | | e.Type = Type.Bool; |
| | | 719 | | |
| | 169190 | 720 | | } else if (expr is BinaryExpr) { |
| | | 721 | | |
| | 60710 | 722 | | BinaryExpr e = (BinaryExpr)expr; |
| | 60710 | 723 | | ResolveExpression(e.E0, resolutionContext); |
| | 60710 | 724 | | Contract.Assert(e.E0.Type != null); // follows from postcondition of ResolveExpression |
| | 60710 | 725 | | ResolveExpression(e.E1, resolutionContext); |
| | 60710 | 726 | | Contract.Assert(e.E1.Type != null); // follows from postcondition of ResolveExpression |
| | | 727 | | |
| | 60710 | 728 | | switch (e.Op) { |
| | | 729 | | case BinaryExpr.Opcode.Iff: |
| | | 730 | | case BinaryExpr.Opcode.Imp: |
| | | 731 | | case BinaryExpr.Opcode.Exp: |
| | | 732 | | case BinaryExpr.Opcode.And: |
| | 15780 | 733 | | case BinaryExpr.Opcode.Or: { |
| | 15780 | 734 | | ConstrainSubtypeRelation(Type.Bool, e.E0.Type, expr, "first argument to {0} must be of type bool (instead |
| | 15780 | 735 | | var secondArgumentDescription = e.E1.tok is QuantifiedVariableRangeToken |
| | 15780 | 736 | | ? "range of quantified variable" : "second argument to {0}"; |
| | 15780 | 737 | | ConstrainSubtypeRelation(Type.Bool, e.E1.Type, expr, secondArgumentDescription + " must be of type bool (i |
| | 15780 | 738 | | expr.Type = Type.Bool; |
| | 15780 | 739 | | break; |
| | | 740 | | } |
| | | 741 | | |
| | | 742 | | case BinaryExpr.Opcode.Eq: |
| | | 743 | | case BinaryExpr.Opcode.Neq: |
| | 2780 | 744 | | AddXConstraint(expr.tok, "Equatable", e.E0.Type, e.E1.Type, "arguments must have comparable types (got {0} a |
| | 2780 | 745 | | expr.Type = Type.Bool; |
| | 2780 | 746 | | break; |
| | | 747 | | |
| | | 748 | | case BinaryExpr.Opcode.Disjoint: |
| | 1070 | 749 | | Type disjointArgumentsType = new InferredTypeProxy(); |
| | 1070 | 750 | | ConstrainSubtypeRelation(disjointArgumentsType, e.E0.Type, expr, "arguments to {2} must have a common supert |
| | 1070 | 751 | | ConstrainSubtypeRelation(disjointArgumentsType, e.E1.Type, expr, "arguments to {2} must have a common supert |
| | 1070 | 752 | | AddXConstraint(expr.tok, "Disjointable", disjointArgumentsType, "arguments must be of a set or multiset type |
| | 1070 | 753 | | expr.Type = Type.Bool; |
| | 1070 | 754 | | break; |
| | | 755 | | |
| | | 756 | | case BinaryExpr.Opcode.Lt: |
| | 22090 | 757 | | case BinaryExpr.Opcode.Le: { |
| | 22090 | 758 | | if (e.Op == BinaryExpr.Opcode.Lt && (PartiallyResolveTypeForMemberSelection(e.E0.tok, e.E0.Type).IsIndData |
| | 0 | 759 | | AddXConstraint(expr.tok, "RankOrderable", e.E0.Type, e.E1.Type, "arguments to rank comparison must be da |
| | 0 | 760 | | e.ResolvedOp = BinaryExpr.ResolvedOpcode.RankLt; |
| | 22090 | 761 | | } else { |
| | 22090 | 762 | | var cmpType = new InferredTypeProxy(); |
| | 22090 | 763 | | var err = new TypeConstraint.ErrorMsgWithToken(expr.tok, "arguments to {2} must have a common supertype |
| | 22090 | 764 | | ConstrainSubtypeRelation(cmpType, e.E0.Type, err); |
| | 22090 | 765 | | ConstrainSubtypeRelation(cmpType, e.E1.Type, err); |
| | 22090 | 766 | | AddXConstraint(expr.tok, "Orderable_Lt", e.E0.Type, e.E1.Type, |
| | 22090 | 767 | | "arguments to " + BinaryExpr.OpcodeString(e.Op) + " must be of a numeric type, bitvector type, ORDINAL |
| | 22090 | 768 | | } |
| | 22090 | 769 | | expr.Type = Type.Bool; |
| | 22090 | 770 | | } |
| | 22090 | 771 | | break; |
| | | 772 | | |
| | | 773 | | case BinaryExpr.Opcode.Gt: |
| | 1300 | 774 | | case BinaryExpr.Opcode.Ge: { |
| | 1300 | 775 | | if (e.Op == BinaryExpr.Opcode.Gt && (PartiallyResolveTypeForMemberSelection(e.E0.tok, e.E0.Type).IsIndData |
| | 0 | 776 | | AddXConstraint(expr.tok, "RankOrderable", e.E1.Type, e.E0.Type, "arguments to rank comparison must be da |
| | 0 | 777 | | e.ResolvedOp = BinaryExpr.ResolvedOpcode.RankGt; |
| | 1300 | 778 | | } else { |
| | 1300 | 779 | | var cmpType = new InferredTypeProxy(); |
| | 1300 | 780 | | var err = new TypeConstraint.ErrorMsgWithToken(expr.tok, "arguments to {2} must have a common supertype |
| | 1300 | 781 | | ConstrainSubtypeRelation(cmpType, e.E0.Type, err); |
| | 1300 | 782 | | ConstrainSubtypeRelation(cmpType, e.E1.Type, err); |
| | 1300 | 783 | | AddXConstraint(expr.tok, "Orderable_Gt", e.E0.Type, e.E1.Type, |
| | 1300 | 784 | | "arguments to " + BinaryExpr.OpcodeString(e.Op) + " must be of a numeric type, bitvector type, ORDINAL |
| | 1300 | 785 | | } |
| | 1300 | 786 | | expr.Type = Type.Bool; |
| | 1300 | 787 | | } |
| | 1300 | 788 | | break; |
| | | 789 | | |
| | | 790 | | case BinaryExpr.Opcode.LeftShift: |
| | 0 | 791 | | case BinaryExpr.Opcode.RightShift: { |
| | 0 | 792 | | expr.Type = new InferredTypeProxy(); |
| | 0 | 793 | | AddXConstraint(e.tok, "IsBitvector", expr.Type, "type of " + BinaryExpr.OpcodeString(e.Op) + " must be a b |
| | 0 | 794 | | ConstrainSubtypeRelation(expr.Type, e.E0.Type, expr.tok, "type of left argument to " + BinaryExpr.OpcodeSt |
| | 0 | 795 | | AddXConstraint(expr.tok, "IntLikeOrBitvector", e.E1.Type, "type of right argument to " + BinaryExpr.Opcode |
| | 0 | 796 | | } |
| | 0 | 797 | | break; |
| | | 798 | | |
| | 12740 | 799 | | case BinaryExpr.Opcode.Add: { |
| | 12740 | 800 | | expr.Type = new InferredTypeProxy(); |
| | 12740 | 801 | | AddXConstraint(e.tok, "Plussable", expr.Type, "type of + must be of a numeric type, a bitvector type, ORDI |
| | 12740 | 802 | | ConstrainSubtypeRelation(expr.Type, e.E0.Type, expr.tok, "type of left argument to + ({0}) must agree with |
| | 12740 | 803 | | ConstrainSubtypeRelation(expr.Type, e.E1.Type, expr.tok, "type of right argument to + ({0}) must agree wit |
| | 12740 | 804 | | } |
| | 12740 | 805 | | break; |
| | | 806 | | |
| | 1250 | 807 | | case BinaryExpr.Opcode.Sub: { |
| | 1250 | 808 | | expr.Type = new InferredTypeProxy(); |
| | 1250 | 809 | | AddXConstraint(e.tok, "Minusable", expr.Type, "type of - must be of a numeric type, bitvector type, ORDINA |
| | 1250 | 810 | | ConstrainSubtypeRelation(expr.Type, e.E0.Type, expr.tok, "type of left argument to - ({0}) must agree with |
| | | 811 | | // The following handles map subtraction, but does not in an unfortunately restrictive way. |
| | | 812 | | // First, it would be nice to delay the decision of it this is a map subtraction or not. This settles |
| | | 813 | | // for the simple way to decide based on what is currently known about the result type, which is also |
| | | 814 | | // done, for example, when deciding if "<" denotes rank ordering on datatypes. |
| | | 815 | | // Second, for map subtraction, it would be nice to allow the right-hand operand to be either a set or |
| | | 816 | | // an iset. That would also lead to further complexity in the code, so this code restricts the right-hand |
| | | 817 | | // operand to be a set. |
| | 1250 | 818 | | var eType = PartiallyResolveTypeForMemberSelection(expr.tok, expr.Type).AsMapType; |
| | 1250 | 819 | | if (eType != null) { |
| | | 820 | | // allow "map - set == map" |
| | 0 | 821 | | var expected = new SetType(true, eType.Domain); |
| | 0 | 822 | | ConstrainSubtypeRelation(expected, e.E1.Type, expr.tok, "map subtraction expects right-hand operand to h |
| | 1250 | 823 | | } else { |
| | 1250 | 824 | | ConstrainSubtypeRelation(expr.Type, e.E1.Type, expr.tok, "type of right argument to - ({0}) must agree w |
| | 1250 | 825 | | } |
| | 1250 | 826 | | } |
| | 1250 | 827 | | break; |
| | | 828 | | |
| | 830 | 829 | | case BinaryExpr.Opcode.Mul: { |
| | 830 | 830 | | expr.Type = new InferredTypeProxy(); |
| | 830 | 831 | | AddXConstraint(e.tok, "Mullable", expr.Type, "type of * must be of a numeric type, bitvector type, or a se |
| | 830 | 832 | | ConstrainSubtypeRelation(expr.Type, e.E0.Type, expr.tok, "type of left argument to * ({0}) must agree with |
| | 830 | 833 | | ConstrainSubtypeRelation(expr.Type, e.E1.Type, expr.tok, "type of right argument to * ({0}) must agree wit |
| | 830 | 834 | | } |
| | 830 | 835 | | break; |
| | | 836 | | |
| | | 837 | | case BinaryExpr.Opcode.In: |
| | | 838 | | case BinaryExpr.Opcode.NotIn: |
| | 1190 | 839 | | var subjectDescription = e.E1.tok is QuantifiedVariableDomainToken |
| | 1190 | 840 | | ? "domain of quantified variable" : "second argument to \"" + BinaryExpr.OpcodeString(e.Op) + "\""; |
| | 1190 | 841 | | AddXConstraint(expr.tok, "Innable", e.E1.Type, e.E0.Type, subjectDescription + " must be a set, multiset, or |
| | 1190 | 842 | | expr.Type = Type.Bool; |
| | 1190 | 843 | | break; |
| | | 844 | | |
| | | 845 | | case BinaryExpr.Opcode.Div: |
| | 840 | 846 | | expr.Type = new InferredTypeProxy(); |
| | 840 | 847 | | AddXConstraint(expr.tok, "NumericOrBitvector", expr.Type, "arguments to " + BinaryExpr.OpcodeString(e.Op) + |
| | 840 | 848 | | ConstrainSubtypeRelation(expr.Type, e.E0.Type, |
| | 840 | 849 | | expr, "type of left argument to " + BinaryExpr.OpcodeString(e.Op) + " ({0}) must agree with the result typ |
| | 840 | 850 | | e.E0.Type, expr.Type); |
| | 840 | 851 | | ConstrainSubtypeRelation(expr.Type, e.E1.Type, |
| | 840 | 852 | | expr, "type of right argument to " + BinaryExpr.OpcodeString(e.Op) + " ({0}) must agree with the result ty |
| | 840 | 853 | | e.E1.Type, expr.Type); |
| | 840 | 854 | | break; |
| | | 855 | | |
| | | 856 | | case BinaryExpr.Opcode.Mod: |
| | 840 | 857 | | expr.Type = new InferredTypeProxy(); |
| | 840 | 858 | | AddXConstraint(expr.tok, "IntLikeOrBitvector", expr.Type, "arguments to " + BinaryExpr.OpcodeString(e.Op) + |
| | 840 | 859 | | ConstrainSubtypeRelation(expr.Type, e.E0.Type, |
| | 840 | 860 | | expr, "type of left argument to " + BinaryExpr.OpcodeString(e.Op) + " ({0}) must agree with the result typ |
| | 840 | 861 | | e.E0.Type, expr.Type); |
| | 840 | 862 | | ConstrainSubtypeRelation(expr.Type, e.E1.Type, |
| | 840 | 863 | | expr, "type of right argument to " + BinaryExpr.OpcodeString(e.Op) + " ({0}) must agree with the result ty |
| | 840 | 864 | | e.E1.Type, expr.Type); |
| | 840 | 865 | | break; |
| | | 866 | | |
| | | 867 | | case BinaryExpr.Opcode.BitwiseAnd: |
| | | 868 | | case BinaryExpr.Opcode.BitwiseOr: |
| | | 869 | | case BinaryExpr.Opcode.BitwiseXor: |
| | 0 | 870 | | expr.Type = NewIntegerBasedProxy(expr.tok); |
| | 0 | 871 | | var errFormat = "first argument to " + BinaryExpr.OpcodeString(e.Op) + " must be of a bitvector type (instea |
| | 0 | 872 | | ConstrainSubtypeRelation(expr.Type, e.E0.Type, expr, errFormat, e.E0.Type); |
| | 0 | 873 | | AddXConstraint(expr.tok, "IsBitvector", e.E0.Type, errFormat); |
| | 0 | 874 | | errFormat = "second argument to " + BinaryExpr.OpcodeString(e.Op) + " must be of a bitvector type (instead g |
| | 0 | 875 | | ConstrainSubtypeRelation(expr.Type, e.E1.Type, expr, errFormat, e.E1.Type); |
| | 0 | 876 | | AddXConstraint(expr.tok, "IsBitvector", e.E1.Type, errFormat); |
| | 0 | 877 | | break; |
| | | 878 | | |
| | | 879 | | default: |
| | 0 | 880 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected operator |
| | | 881 | | } |
| | | 882 | | // We should also fill in e.ResolvedOp, but we may not have enough information for that yet. So, instead, delay |
| | | 883 | | // setting e.ResolvedOp until inside CheckTypeInference. |
| | | 884 | | |
| | 108480 | 885 | | } else if (expr is TernaryExpr) { |
| | 0 | 886 | | var e = (TernaryExpr)expr; |
| | 0 | 887 | | ResolveExpression(e.E0, resolutionContext); |
| | 0 | 888 | | ResolveExpression(e.E1, resolutionContext); |
| | 0 | 889 | | ResolveExpression(e.E2, resolutionContext); |
| | 0 | 890 | | switch (e.Op) { |
| | | 891 | | case TernaryExpr.Opcode.PrefixEqOp: |
| | | 892 | | case TernaryExpr.Opcode.PrefixNeqOp: |
| | 0 | 893 | | AddXConstraint(expr.tok, "IntOrORDINAL", e.E0.Type, "prefix-equality limit argument must be an ORDINAL or in |
| | 0 | 894 | | AddXConstraint(expr.tok, "Equatable", e.E1.Type, e.E2.Type, "arguments must have the same type (got {0} and |
| | 0 | 895 | | AddXConstraint(expr.tok, "IsCoDatatype", e.E1.Type, "arguments to prefix equality must be codatatypes (inste |
| | 0 | 896 | | expr.Type = Type.Bool; |
| | 0 | 897 | | break; |
| | | 898 | | default: |
| | 0 | 899 | | Contract.Assert(false); // unexpected ternary operator |
| | 0 | 900 | | break; |
| | | 901 | | } |
| | | 902 | | |
| | 81670 | 903 | | } else if (expr is LetExpr) { |
| | 33900 | 904 | | var e = (LetExpr)expr; |
| | 67800 | 905 | | if (e.Exact) { |
| | 203400 | 906 | | foreach (var rhs in e.RHSs) { |
| | 33900 | 907 | | ResolveExpression(rhs, resolutionContext); |
| | 33900 | 908 | | } |
| | 33900 | 909 | | scope.PushMarker(); |
| | 33900 | 910 | | if (e.LHSs.Count != e.RHSs.Count) { |
| | 0 | 911 | | reporter.Error(MessageSource.Resolver, expr, "let expression must have same number of LHSs (found {0}) as RH |
| | 0 | 912 | | } |
| | 33900 | 913 | | var i = 0; |
| | 203400 | 914 | | foreach (var lhs in e.LHSs) { |
| | 33900 | 915 | | var rhsType = i < e.RHSs.Count ? e.RHSs[i].Type : new InferredTypeProxy(); |
| | 33900 | 916 | | ResolveCasePattern(lhs, rhsType, resolutionContext); |
| | | 917 | | // Check for duplicate names now, because not until after resolving the case pattern do we know if identifie |
| | 33900 | 918 | | var c = 0; |
| | 203400 | 919 | | foreach (var v in lhs.Vars) { |
| | 33900 | 920 | | ScopePushAndReport(scope, v, "let-variable"); |
| | 33900 | 921 | | c++; |
| | 33900 | 922 | | } |
| | 33900 | 923 | | if (c == 0) { |
| | | 924 | | // Every identifier-looking thing in the pattern resolved to a constructor; that is, this LHS is a constan |
| | 0 | 925 | | reporter.Error(MessageSource.Resolver, lhs.tok, "LHS is a constant literal; to be legal, it must introduce |
| | 0 | 926 | | } |
| | 33900 | 927 | | i++; |
| | 33900 | 928 | | } |
| | 33900 | 929 | | } else { |
| | | 930 | | // let-such-that expression |
| | 0 | 931 | | if (e.RHSs.Count != 1) { |
| | 0 | 932 | | reporter.Error(MessageSource.Resolver, expr, "let-such-that expression must have just one RHS (found {0})", |
| | 0 | 933 | | } |
| | | 934 | | // the bound variables are in scope in the RHS of a let-such-that expression |
| | 0 | 935 | | scope.PushMarker(); |
| | 0 | 936 | | foreach (var lhs in e.LHSs) { |
| | 0 | 937 | | Contract.Assert(lhs.Var != null); // the parser already checked that every LHS is a BoundVar, not a general |
| | 0 | 938 | | var v = lhs.Var; |
| | 0 | 939 | | ScopePushAndReport(scope, v, "let-variable"); |
| | 0 | 940 | | ResolveType(v.tok, v.Type, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 941 | | } |
| | 0 | 942 | | foreach (var rhs in e.RHSs) { |
| | 0 | 943 | | ResolveExpression(rhs, resolutionContext); |
| | 0 | 944 | | ConstrainTypeExprBool(rhs, "type of RHS of let-such-that expression must be boolean (got {0})"); |
| | 0 | 945 | | } |
| | 0 | 946 | | } |
| | 33900 | 947 | | ResolveExpression(e.Body, resolutionContext); |
| | 33900 | 948 | | ResolveAttributes(e, resolutionContext); |
| | 33900 | 949 | | scope.PopMarker(); |
| | 33900 | 950 | | expr.Type = e.Body.Type; |
| | 47770 | 951 | | } else if (expr is LetOrFailExpr) { |
| | 0 | 952 | | var e = (LetOrFailExpr)expr; |
| | 0 | 953 | | ResolveLetOrFailExpr(e, resolutionContext); |
| | 13870 | 954 | | } else if (expr is QuantifierExpr) { |
| | 0 | 955 | | var e = (QuantifierExpr)expr; |
| | 0 | 956 | | Contract.Assert(e.SplitQuantifier == null); // No split quantifiers during resolution |
| | 0 | 957 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 958 | | scope.PushMarker(); |
| | 0 | 959 | | foreach (BoundVar v in e.BoundVars) { |
| | 0 | 960 | | ScopePushAndReport(scope, v, "bound-variable"); |
| | 0 | 961 | | var option = new ResolveTypeOption(ResolveTypeOptionEnum.InferTypeProxies); |
| | 0 | 962 | | ResolveType(v.tok, v.Type, resolutionContext, option, null); |
| | 0 | 963 | | } |
| | 0 | 964 | | if (e.Range != null) { |
| | 0 | 965 | | ResolveExpression(e.Range, resolutionContext); |
| | 0 | 966 | | Contract.Assert(e.Range.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 967 | | ConstrainTypeExprBool(e.Range, "range of quantifier must be of type bool (instead got {0})"); |
| | 0 | 968 | | } |
| | 0 | 969 | | ResolveExpression(e.Term, resolutionContext); |
| | 0 | 970 | | Contract.Assert(e.Term.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 971 | | ConstrainTypeExprBool(e.Term, "body of quantifier must be of type bool (instead got {0})"); |
| | | 972 | | // Since the body is more likely to infer the types of the bound variables, resolve it |
| | | 973 | | // first (above) and only then resolve the attributes (below). |
| | 0 | 974 | | ResolveAttributes(e, resolutionContext); |
| | 0 | 975 | | scope.PopMarker(); |
| | 0 | 976 | | expr.Type = Type.Bool; |
| | | 977 | | |
| | 13870 | 978 | | } else if (expr is SetComprehension) { |
| | 0 | 979 | | var e = (SetComprehension)expr; |
| | 0 | 980 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 981 | | scope.PushMarker(); |
| | 0 | 982 | | foreach (BoundVar v in e.BoundVars) { |
| | 0 | 983 | | ScopePushAndReport(scope, v, "bound-variable"); |
| | 0 | 984 | | ResolveType(v.tok, v.Type, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 985 | | var inferredProxy = v.Type as InferredTypeProxy; |
| | 0 | 986 | | if (inferredProxy != null) { |
| | 0 | 987 | | Contract.Assert(!inferredProxy.KeepConstraints); // in general, this proxy is inferred to be a base type |
| | 0 | 988 | | } |
| | 0 | 989 | | } |
| | 0 | 990 | | ResolveExpression(e.Range, resolutionContext); |
| | 0 | 991 | | Contract.Assert(e.Range.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 992 | | ConstrainTypeExprBool(e.Range, "range of comprehension must be of type bool (instead got {0})"); |
| | 0 | 993 | | ResolveExpression(e.Term, resolutionContext); |
| | 0 | 994 | | Contract.Assert(e.Term.Type != null); // follows from postcondition of ResolveExpression |
| | | 995 | | |
| | 0 | 996 | | ResolveAttributes(e, resolutionContext); |
| | 0 | 997 | | scope.PopMarker(); |
| | 0 | 998 | | expr.Type = new SetType(e.Finite, e.Term.Type); |
| | | 999 | | |
| | 13870 | 1000 | | } else if (expr is MapComprehension) { |
| | 0 | 1001 | | var e = (MapComprehension)expr; |
| | 0 | 1002 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 1003 | | scope.PushMarker(); |
| | 0 | 1004 | | Contract.Assert(e.BoundVars.Count == 1 || (1 < e.BoundVars.Count && e.TermLeft != null)); |
| | 0 | 1005 | | foreach (BoundVar v in e.BoundVars) { |
| | 0 | 1006 | | ScopePushAndReport(scope, v, "bound-variable"); |
| | 0 | 1007 | | ResolveType(v.tok, v.Type, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 1008 | | var inferredProxy = v.Type as InferredTypeProxy; |
| | 0 | 1009 | | if (inferredProxy != null) { |
| | 0 | 1010 | | Contract.Assert(!inferredProxy.KeepConstraints); // in general, this proxy is inferred to be a base type |
| | 0 | 1011 | | } |
| | 0 | 1012 | | } |
| | 0 | 1013 | | ResolveExpression(e.Range, resolutionContext); |
| | 0 | 1014 | | Contract.Assert(e.Range.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 1015 | | ConstrainTypeExprBool(e.Range, "range of comprehension must be of type bool (instead got {0})"); |
| | 0 | 1016 | | if (e.TermLeft != null) { |
| | 0 | 1017 | | ResolveExpression(e.TermLeft, resolutionContext); |
| | 0 | 1018 | | Contract.Assert(e.TermLeft.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 1019 | | } |
| | 0 | 1020 | | ResolveExpression(e.Term, resolutionContext); |
| | 0 | 1021 | | Contract.Assert(e.Term.Type != null); // follows from postcondition of ResolveExpression |
| | | 1022 | | |
| | 0 | 1023 | | ResolveAttributes(e, resolutionContext); |
| | 0 | 1024 | | scope.PopMarker(); |
| | 0 | 1025 | | expr.Type = new MapType(e.Finite, e.TermLeft != null ? e.TermLeft.Type : e.BoundVars[0].Type, e.Term.Type); |
| | | 1026 | | |
| | 18500 | 1027 | | } else if (expr is LambdaExpr) { |
| | 4630 | 1028 | | var e = (LambdaExpr)expr; |
| | 4630 | 1029 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 4630 | 1030 | | scope.PushMarker(); |
| | 45450 | 1031 | | foreach (BoundVar v in e.BoundVars) { |
| | 10520 | 1032 | | ScopePushAndReport(scope, v, "bound-variable"); |
| | 10520 | 1033 | | ResolveType(v.tok, v.Type, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 10520 | 1034 | | } |
| | | 1035 | | |
| | 4630 | 1036 | | if (e.Range != null) { |
| | 0 | 1037 | | ResolveExpression(e.Range, resolutionContext); |
| | 0 | 1038 | | Contract.Assert(e.Range.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 1039 | | ConstrainTypeExprBool(e.Range, "Precondition must be boolean (got {0})"); |
| | 0 | 1040 | | } |
| | 13890 | 1041 | | foreach (var read in e.Reads) { |
| | 0 | 1042 | | ResolveFrameExpression(read, FrameExpressionUse.Reads, resolutionContext); |
| | 0 | 1043 | | } |
| | 4630 | 1044 | | ResolveExpression(e.Term, resolutionContext); |
| | 4630 | 1045 | | Contract.Assert(e.Term.Type != null); |
| | 4630 | 1046 | | scope.PopMarker(); |
| | 15150 | 1047 | | expr.Type = SelectAppropriateArrowType(e.tok, e.BoundVars.ConvertAll(v => v.Type), e.Body.Type, e.Reads.Count != |
| | 13870 | 1048 | | } else if (expr is WildcardExpr) { |
| | 0 | 1049 | | expr.Type = new SetType(true, builtIns.ObjectQ()); |
| | 9240 | 1050 | | } else if (expr is StmtExpr) { |
| | 0 | 1051 | | var e = (StmtExpr)expr; |
| | 0 | 1052 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | | 1053 | | |
| | 0 | 1054 | | ResolveStatement(e.S, resolutionContext); |
| | 0 | 1055 | | if (reporter.Count(ErrorLevel.Error) == prevErrorCount) { |
| | 0 | 1056 | | var r = e.S as UpdateStmt; |
| | 0 | 1057 | | if (r != null && r.ResolvedStatements.Count == 1) { |
| | 0 | 1058 | | var call = r.ResolvedStatements[0] as CallStmt; |
| | 0 | 1059 | | if (call.Method is TwoStateLemma && !resolutionContext.IsTwoState) { |
| | 0 | 1060 | | reporter.Error(MessageSource.Resolver, call, "two-state lemmas can only be used in two-state contexts"); |
| | 0 | 1061 | | } |
| | 0 | 1062 | | } |
| | 0 | 1063 | | } |
| | | 1064 | | |
| | 0 | 1065 | | ResolveExpression(e.E, resolutionContext); |
| | 0 | 1066 | | Contract.Assert(e.E.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 1067 | | expr.Type = e.E.Type; |
| | | 1068 | | |
| | 18480 | 1069 | | } else if (expr is ITEExpr) { |
| | 9240 | 1070 | | ITEExpr e = (ITEExpr)expr; |
| | 9240 | 1071 | | ResolveExpression(e.Test, resolutionContext); |
| | 9240 | 1072 | | Contract.Assert(e.Test.Type != null); // follows from postcondition of ResolveExpression |
| | 9240 | 1073 | | ResolveExpression(e.Thn, resolutionContext); |
| | 9240 | 1074 | | Contract.Assert(e.Thn.Type != null); // follows from postcondition of ResolveExpression |
| | 9240 | 1075 | | ResolveExpression(e.Els, resolutionContext); |
| | 9240 | 1076 | | Contract.Assert(e.Els.Type != null); // follows from postcondition of ResolveExpression |
| | 9240 | 1077 | | ConstrainTypeExprBool(e.Test, "guard condition in if-then-else expression must be a boolean (instead got {0})"); |
| | 9240 | 1078 | | expr.Type = new InferredTypeProxy(); |
| | 9240 | 1079 | | ConstrainSubtypeRelation(expr.Type, e.Thn.Type, expr, "the two branches of an if-then-else expression must have |
| | 9240 | 1080 | | ConstrainSubtypeRelation(expr.Type, e.Els.Type, expr, "the two branches of an if-then-else expression must have |
| | | 1081 | | |
| | 9240 | 1082 | | } else if (expr is NestedMatchExpr nestedMatchExpr) { |
| | 0 | 1083 | | ResolveNestedMatchExpr(nestedMatchExpr, resolutionContext); |
| | 0 | 1084 | | } else { |
| | 0 | 1085 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected expression |
| | | 1086 | | } |
| | | 1087 | | |
| | 1447340 | 1088 | | if (expr.Type == null) { |
| | | 1089 | | // some resolution error occurred |
| | 0 | 1090 | | expr.Type = new InferredTypeProxy(); |
| | 0 | 1091 | | } |
| | | 1092 | | |
| | 1447340 | 1093 | | DominatingStatementLabels.PopMarker(); |
| | 2458360 | 1094 | | } |
| | | 1095 | | |
| | 40940 | 1096 | | void ResolveTypeParameters(List<TypeParameter/*!*/>/*!*/ tparams, bool emitErrors, TypeParameter.ParentType/*!*/ par |
| | | 1097 | | Contract.Requires(tparams != null); |
| | | 1098 | | Contract.Requires(parent != null); |
| | | 1099 | | // push non-duplicated type parameter names |
| | 40940 | 1100 | | int index = 0; |
| | 174480 | 1101 | | foreach (TypeParameter tp in tparams) { |
| | 26040 | 1102 | | if (emitErrors) { |
| | | 1103 | | // we're seeing this TypeParameter for the first time |
| | 8820 | 1104 | | tp.Parent = parent; |
| | 8820 | 1105 | | tp.PositionalIndex = index; |
| | 8820 | 1106 | | } |
| | 17220 | 1107 | | var r = allTypeParameters.Push(tp.Name, tp); |
| | 26040 | 1108 | | if (emitErrors) { |
| | 8820 | 1109 | | if (r == Scope<TypeParameter>.PushResult.Duplicate) { |
| | 0 | 1110 | | reporter.Error(MessageSource.Resolver, ErrorRegistry.NoneId, tp, "Duplicate type-parameter name: {0}", tp.Na |
| | 8820 | 1111 | | } else if (r == Scope<TypeParameter>.PushResult.Shadow) { |
| | 0 | 1112 | | reporter.Warning(MessageSource.Resolver, ErrorRegistry.NoneId, tp.tok, "Shadowed type-parameter name: {0}", |
| | 0 | 1113 | | } |
| | 8820 | 1114 | | } |
| | 17220 | 1115 | | } |
| | 40940 | 1116 | | } |
| | | 1117 | | |
| | 97720 | 1118 | | private bool ConstrainSubtypeRelation(Type super, Type sub, Expression exprForToken, string msg, params object[] msg |
| | | 1119 | | Contract.Requires(sub != null); |
| | | 1120 | | Contract.Requires(super != null); |
| | | 1121 | | Contract.Requires(exprForToken != null); |
| | | 1122 | | Contract.Requires(msg != null); |
| | | 1123 | | Contract.Requires(msgArgs != null); |
| | 97720 | 1124 | | return ConstrainSubtypeRelation(super, sub, exprForToken.tok, msg, msgArgs); |
| | 97720 | 1125 | | } |
| | | 1126 | | |
| | 22860 | 1127 | | public void ConstrainTypeExprBool(Expression e, string msg) { |
| | | 1128 | | Contract.Requires(e != null); |
| | | 1129 | | Contract.Requires(msg != null); // expected to have a {0} part |
| | 22860 | 1130 | | ConstrainSubtypeRelation(Type.Bool, e.Type, e, msg, e.Type); |
| | 22860 | 1131 | | } |
| | | 1132 | | |
| | 558550 | 1133 | | public bool ConstrainSubtypeRelation(Type super, Type sub, IToken tok, string msg, params object[] msgArgs) { |
| | | 1134 | | Contract.Requires(sub != null); |
| | | 1135 | | Contract.Requires(super != null); |
| | | 1136 | | Contract.Requires(tok != null); |
| | | 1137 | | Contract.Requires(msg != null); |
| | | 1138 | | Contract.Requires(msgArgs != null); |
| | 558550 | 1139 | | return ConstrainSubtypeRelation(super, sub, new TypeConstraint.ErrorMsgWithToken(tok, msg, msgArgs)); |
| | 558550 | 1140 | | } |
| | | 1141 | | |
| | 446220 | 1142 | | private void ConstrainAssignable(NonProxyType lhs, Type rhs, TypeConstraint.ErrorMsg errMsg, out bool moreXConstrain |
| | | 1143 | | Contract.Requires(lhs != null); |
| | | 1144 | | Contract.Requires(rhs != null); |
| | | 1145 | | Contract.Requires(errMsg != null); |
| | | 1146 | | |
| | 446220 | 1147 | | DetermineRootLeaf(lhs, out var isRoot, out _, out _, out _); |
| | 890430 | 1148 | | if (isRoot) { |
| | 444210 | 1149 | | ConstrainSubtypeRelation(lhs, rhs, errMsg, true, allowDecisions); |
| | 444210 | 1150 | | moreXConstraints = false; |
| | 446220 | 1151 | | } else { |
| | 2010 | 1152 | | var lhsWithProxyArgs = Type.HeadWithProxyArgs(lhs); |
| | 2010 | 1153 | | ConstrainSubtypeRelation(lhsWithProxyArgs, rhs, errMsg, false, allowDecisions); |
| | 2010 | 1154 | | ConstrainAssignableTypeArgs(lhs, lhsWithProxyArgs.TypeArgs, lhs.TypeArgs, errMsg, out moreXConstraints); |
| | 3390 | 1155 | | if (lhs.AsCollectionType == null) { |
| | 1380 | 1156 | | var sameHead = Type.SameHead(lhs, rhs); |
| | 1380 | 1157 | | if (!sameHead && lhs is UserDefinedType udtLhs && rhs is UserDefinedType udtRhs) { |
| | | 1158 | | // also allow the case where lhs is a possibly-null type and rhs is a non-null type |
| | 0 | 1159 | | sameHead = udtLhs.ResolvedClass == (udtRhs.ResolvedClass as NonNullTypeDecl)?.Class; |
| | 0 | 1160 | | } |
| | 2760 | 1161 | | if (sameHead) { |
| | 1380 | 1162 | | ConstrainAssignableTypeArgs(lhs, lhs.TypeArgs, rhs.TypeArgs, errMsg, out var more2); |
| | 1380 | 1163 | | moreXConstraints = moreXConstraints || more2; |
| | 1380 | 1164 | | } |
| | 1380 | 1165 | | } |
| | 2010 | 1166 | | } |
| | 446220 | 1167 | | } |
| | | 1168 | | |
| | 331940 | 1169 | | private void ConstrainAssignableTypeArgs(Type typeHead, List<Type> A, List<Type> B, TypeConstraint.ErrorMsg errMsg, |
| | | 1170 | | Contract.Requires(typeHead != null); |
| | | 1171 | | Contract.Requires(A != null); |
| | | 1172 | | Contract.Requires(B != null); |
| | | 1173 | | Contract.Requires(A.Count == B.Count); |
| | | 1174 | | Contract.Requires(errMsg != null); |
| | | 1175 | | |
| | 331940 | 1176 | | var tok = errMsg.Tok; |
| | 516140 | 1177 | | if (B.Count == 0) { |
| | | 1178 | | // all done |
| | 184200 | 1179 | | moreXConstraints = false; |
| | 331940 | 1180 | | } else if (typeHead is MapType) { |
| | 0 | 1181 | | var em = new TypeConstraint.ErrorMsgWithBase(errMsg, "covariance for type parameter at index 0 expects {1} <: {0 |
| | 0 | 1182 | | AddAssignableConstraint(tok, A[0], B[0], em); |
| | 0 | 1183 | | em = new TypeConstraint.ErrorMsgWithBase(errMsg, "covariance for type parameter at index 1 expects {1} <: {0}", |
| | 0 | 1184 | | AddAssignableConstraint(tok, A[1], B[1], em); |
| | 0 | 1185 | | moreXConstraints = true; |
| | 184340 | 1186 | | } else if (typeHead is CollectionType) { |
| | 36600 | 1187 | | var em = new TypeConstraint.ErrorMsgWithBase(errMsg, "covariance for type parameter expects {1} <: {0}", A[0], B |
| | 36600 | 1188 | | AddAssignableConstraint(tok, A[0], B[0], em); |
| | 36600 | 1189 | | moreXConstraints = true; |
| | 147740 | 1190 | | } else { |
| | 111140 | 1191 | | var udt = (UserDefinedType)typeHead; // note, collections, maps, and user-defined types are the only one with T |
| | 111140 | 1192 | | var cl = udt.ResolvedClass; |
| | 111140 | 1193 | | Contract.Assert(cl != null); |
| | 111140 | 1194 | | Contract.Assert(cl.TypeArgs.Count == B.Count); |
| | 111140 | 1195 | | moreXConstraints = false; |
| | 1079110 | 1196 | | for (int i = 0; i < B.Count; i++) { |
| | 285610 | 1197 | | var msgFormat = "variance for type parameter" + (B.Count == 1 ? "" : " at index " + i) + " expects {0} {1} {2} |
| | 566530 | 1198 | | if (cl.TypeArgs[i].Variance == TypeParameter.TPVariance.Co) { |
| | 280920 | 1199 | | var em = new TypeConstraint.ErrorMsgWithBase(errMsg, "co" + msgFormat, A[i], ":>", B[i]); |
| | 280920 | 1200 | | AddAssignableConstraint(tok, A[i], B[i], em); |
| | 280920 | 1201 | | moreXConstraints = true; |
| | 290300 | 1202 | | } else if (cl.TypeArgs[i].Variance == TypeParameter.TPVariance.Contra) { |
| | 4690 | 1203 | | var em = new TypeConstraint.ErrorMsgWithBase(errMsg, "contra" + msgFormat, A[i], "<:", B[i]); |
| | 4690 | 1204 | | AddAssignableConstraint(tok, B[i], A[i], em); |
| | 4690 | 1205 | | moreXConstraints = true; |
| | 4690 | 1206 | | } else { |
| | 0 | 1207 | | var em = new TypeConstraint.ErrorMsgWithBase(errMsg, "non" + msgFormat, A[i], "=", B[i]); |
| | 0 | 1208 | | ConstrainSubtypeRelation_Equal(A[i], B[i], em); |
| | 0 | 1209 | | } |
| | 285610 | 1210 | | } |
| | 111140 | 1211 | | } |
| | 331940 | 1212 | | } |
| | | 1213 | | |
| | | 1214 | | /// <summary> |
| | | 1215 | | /// Adds the subtyping constraint that "a" and "b" are the same type. |
| | | 1216 | | /// </summary> |
| | 400 | 1217 | | private void ConstrainSubtypeRelation_Equal(Type a, Type b, TypeConstraint.ErrorMsg errMsg) { |
| | | 1218 | | Contract.Requires(a != null); |
| | | 1219 | | Contract.Requires(b != null); |
| | | 1220 | | Contract.Requires(errMsg != null); |
| | | 1221 | | |
| | 400 | 1222 | | var proxy = a.Normalize() as TypeProxy; |
| | 510 | 1223 | | if (proxy != null && proxy.T == null && !Reaches(b, proxy, 1, new HashSet<TypeProxy>())) { |
| | 110 | 1224 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 1225 | | Options.OutputWriter.WriteLine("DEBUG: (invariance) assigning proxy {0}.T := {1}", proxy, b); |
| | 0 | 1226 | | } |
| | 110 | 1227 | | proxy.T = b; |
| | 110 | 1228 | | } |
| | 400 | 1229 | | proxy = b.Normalize() as TypeProxy; |
| | 660 | 1230 | | if (proxy != null && proxy.T == null && !Reaches(a, proxy, 1, new HashSet<TypeProxy>())) { |
| | 260 | 1231 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 1232 | | Options.OutputWriter.WriteLine("DEBUG: (invariance) assigning proxy {0}.T := {1}", proxy, a); |
| | 0 | 1233 | | } |
| | 260 | 1234 | | proxy.T = a; |
| | 260 | 1235 | | } |
| | | 1236 | | |
| | 400 | 1237 | | ConstrainSubtypeRelation(a, b, errMsg, true); |
| | 400 | 1238 | | ConstrainSubtypeRelation(b, a, errMsg, true); |
| | 400 | 1239 | | } |
| | | 1240 | | |
| | | 1241 | | /// <summary> |
| | | 1242 | | /// Adds the subtyping constraint that "sub" is a subtype of "super". |
| | | 1243 | | /// If this constraint seems feasible, returns "true". Otherwise, prints error message (either "errMsg" or somethin |
| | | 1244 | | /// more specific) and returns "false". |
| | | 1245 | | /// Note, if in doubt, this method can return "true", because the constraints will be checked for sure at a later st |
| | | 1246 | | /// </summary> |
| | 2643960 | 1247 | | private bool ConstrainSubtypeRelation(Type super, Type sub, TypeConstraint.ErrorMsg errMsg, bool keepConstraints = f |
| | | 1248 | | Contract.Requires(sub != null); |
| | | 1249 | | Contract.Requires(super != null); |
| | | 1250 | | Contract.Requires(errMsg != null); |
| | | 1251 | | |
| | 3086660 | 1252 | | if (!keepConstraints && super is InferredTypeProxy) { |
| | 442700 | 1253 | | var ip = (InferredTypeProxy)super; |
| | 785170 | 1254 | | if (ip.KeepConstraints) { |
| | 342470 | 1255 | | keepConstraints = true; |
| | 342470 | 1256 | | } |
| | 442700 | 1257 | | } |
| | 2772810 | 1258 | | if (!keepConstraints && sub is InferredTypeProxy) { |
| | 128850 | 1259 | | var ip = (InferredTypeProxy)sub; |
| | 135775 | 1260 | | if (ip.KeepConstraints) { |
| | 6925 | 1261 | | keepConstraints = true; |
| | 6925 | 1262 | | } |
| | 128850 | 1263 | | } |
| | | 1264 | | |
| | 2643960 | 1265 | | super = super.NormalizeExpand(keepConstraints); |
| | 2643960 | 1266 | | sub = sub.NormalizeExpand(keepConstraints); |
| | 2643960 | 1267 | | var c = new TypeConstraint(super, sub, errMsg, keepConstraints); |
| | 2643960 | 1268 | | AllTypeConstraints.Add(c); |
| | 2643960 | 1269 | | return ConstrainSubtypeRelation_Aux(super, sub, c, keepConstraints, allowDecisions); |
| | 2643960 | 1270 | | } |
| | 2643960 | 1271 | | private bool ConstrainSubtypeRelation_Aux(Type super, Type sub, TypeConstraint c, bool keepConstraints, bool allowDe |
| | | 1272 | | Contract.Requires(sub != null); |
| | | 1273 | | Contract.Requires(!(sub is TypeProxy) || ((TypeProxy)sub).T == null); // caller is expected to have Normalized aw |
| | | 1274 | | Contract.Requires(super != null); |
| | | 1275 | | Contract.Requires(!(super is TypeProxy) || ((TypeProxy)super).T == null); // caller is expected to have Normalize |
| | | 1276 | | Contract.Requires(c != null); |
| | | 1277 | | |
| | 3552400 | 1278 | | if (object.ReferenceEquals(super, sub)) { |
| | 908440 | 1279 | | return true; |
| | 2427450 | 1280 | | } else if (super is TypeProxy && sub is TypeProxy) { |
| | | 1281 | | // both are proxies |
| | 691930 | 1282 | | ((TypeProxy)sub).AddSupertype(c); |
| | 691930 | 1283 | | ((TypeProxy)super).AddSubtype(c); |
| | 691930 | 1284 | | return true; |
| | 1361210 | 1285 | | } else if (sub is TypeProxy) { |
| | 317620 | 1286 | | var proxy = (TypeProxy)sub; |
| | 317620 | 1287 | | proxy.AddSupertype(c); |
| | 317620 | 1288 | | AssignKnownEnd(proxy, keepConstraints, allowDecisions); |
| | 317620 | 1289 | | return true; |
| | 1155350 | 1290 | | } else if (super is TypeProxy) { |
| | 429380 | 1291 | | var proxy = (TypeProxy)super; |
| | 429380 | 1292 | | proxy.AddSubtype(c); |
| | 429380 | 1293 | | AssignKnownEnd(proxy, keepConstraints, allowDecisions); |
| | 429380 | 1294 | | return true; |
| | 296590 | 1295 | | } else { |
| | | 1296 | | // two non-proxy types |
| | | 1297 | | // set "headSymbolsAgree" to "false" if it's clear the head symbols couldn't be the same; "true" means they may |
| | 296590 | 1298 | | bool headSymbolsAgree = Type.IsHeadSupertypeOf(super.NormalizeExpand(keepConstraints), sub); |
| | 296590 | 1299 | | if (!headSymbolsAgree) { |
| | 0 | 1300 | | c.FlagAsError(this); |
| | 0 | 1301 | | return false; |
| | | 1302 | | } |
| | | 1303 | | // TODO: inspect type parameters in order to produce some error messages sooner |
| | 296590 | 1304 | | return true; |
| | | 1305 | | } |
| | 2643960 | 1306 | | } |
| | | 1307 | | |
| | | 1308 | | /// <summary> |
| | | 1309 | | /// "root" says that the type is a non-artificial type (that is, not an ArtificialType) with no proper supertypes. |
| | | 1310 | | /// "leaf" says that the only possible proper subtypes are subset types of the type. Thus, the only |
| | | 1311 | | /// types that are not leaf types are traits and artificial types. |
| | | 1312 | | /// The "headIs" versions speak only about the head symbols, so it is possible that the given |
| | | 1313 | | /// type arguments would change the root/leaf status of the entire type. |
| | | 1314 | | /// </summary> |
| | 1015011230 | 1315 | | void DetermineRootLeaf(Type t, out bool isRoot, out bool isLeaf, out bool headIsRoot, out bool headIsLeaf) { |
| | | 1316 | | Contract.Requires(t != null); |
| | | 1317 | | Contract.Ensures(!Contract.ValueAtReturn(out isRoot) || Contract.ValueAtReturn(out headIsRoot)); // isRoot ==> hea |
| | | 1318 | | Contract.Ensures(!Contract.ValueAtReturn(out isLeaf) || Contract.ValueAtReturn(out headIsLeaf)); // isLeaf ==> hea |
| | 1015011230 | 1319 | | t = t.NormalizeExpandKeepConstraints(); |
| | 1015011650 | 1320 | | if (t.IsObjectQ) { |
| | 840 | 1321 | | isRoot = true; isLeaf = false; |
| | 840 | 1322 | | headIsRoot = true; headIsLeaf = false; |
| | 1015015990 | 1323 | | } else if (t is ArrowType) { |
| | 4760 | 1324 | | var arr = (ArrowType)t; |
| | 9520 | 1325 | | headIsRoot = true; headIsLeaf = true; // these are definitely true |
| | 9520 | 1326 | | isRoot = true; isLeaf = true; // set these to true until proven otherwise |
| | 4760 | 1327 | | Contract.Assert(arr.Arity + 1 == arr.TypeArgs.Count); |
| | 54370 | 1328 | | for (int i = 0; i < arr.TypeArgs.Count; i++) { |
| | 14950 | 1329 | | var arg = arr.TypeArgs[i]; |
| | 14950 | 1330 | | DetermineRootLeaf(arg, out var r, out var l, out _, out _); |
| | 25140 | 1331 | | if (i < arr.Arity) { |
| | 20380 | 1332 | | isRoot &= l; isLeaf &= r; // argument types are contravariant |
| | 14950 | 1333 | | } else { |
| | 9520 | 1334 | | isRoot &= r; isLeaf &= l; // result type is covariant |
| | 4760 | 1335 | | } |
| | 14950 | 1336 | | } |
| | 1023434450 | 1337 | | } else if (t is UserDefinedType) { |
| | 8423640 | 1338 | | var udt = (UserDefinedType)t; |
| | 8423640 | 1339 | | var cl = udt.ResolvedClass; |
| | 16847280 | 1340 | | if (cl != null) { |
| | 8470680 | 1341 | | if (cl is TypeParameter) { |
| | 47040 | 1342 | | var tp = udt.AsTypeParameter; |
| | 47040 | 1343 | | Contract.Assert(tp != null); |
| | 94080 | 1344 | | headIsRoot = true; headIsLeaf = true; // all type parameters are non-variant |
| | 10359290 | 1345 | | } else if (cl is SubsetTypeDecl) { |
| | 3871300 | 1346 | | headIsRoot = false; headIsLeaf = true; |
| | 8376600 | 1347 | | } else if (cl is NewtypeDecl) { |
| | 0 | 1348 | | headIsRoot = true; headIsLeaf = true; |
| | 6440950 | 1349 | | } else if (cl is TraitDecl) { |
| | 0 | 1350 | | headIsRoot = false; headIsLeaf = false; |
| | 6441370 | 1351 | | } else if (cl is ClassDecl) { |
| | 840 | 1352 | | headIsRoot = false; headIsLeaf = true; |
| | 6440950 | 1353 | | } else if (cl is AbstractTypeDecl) { |
| | 0 | 1354 | | headIsRoot = true; headIsLeaf = true; |
| | 6440530 | 1355 | | } else if (cl is InternalTypeSynonymDecl) { |
| | 0 | 1356 | | Contract.Assert(object.ReferenceEquals(t, t.NormalizeExpand())); // should be opaque in scope |
| | 0 | 1357 | | headIsRoot = true; headIsLeaf = true; |
| | 6440530 | 1358 | | } else { |
| | 6440530 | 1359 | | Contract.Assert(cl is DatatypeDecl); |
| | 12881060 | 1360 | | headIsRoot = true; headIsLeaf = true; |
| | 6440530 | 1361 | | } |
| | | 1362 | | // for "isRoot" and "isLeaf", also take into consideration the root/leaf status of type arguments |
| | 16847280 | 1363 | | isRoot = headIsRoot; isLeaf = headIsLeaf; |
| | 8423640 | 1364 | | Contract.Assert(udt.TypeArgs.Count == cl.TypeArgs.Count); |
| | 69694620 | 1365 | | for (int i = 0; i < udt.TypeArgs.Count; i++) { |
| | 17615780 | 1366 | | var variance = cl.TypeArgs[i].Variance; |
| | 35231140 | 1367 | | if (variance != TypeParameter.TPVariance.Non) { |
| | 17615360 | 1368 | | DetermineRootLeaf(udt.TypeArgs[i], out var r, out var l, out _, out _); |
| | | 1369 | | // isRoot and isLeaf aren't duals, so Co and Contra require separate consideration beyond inversion. |
| | 17615360 | 1370 | | switch (variance) { |
| | 55577880 | 1371 | | case TypeParameter.TPVariance.Co: { isRoot &= r; isLeaf &= l; break; } |
| | | 1372 | | // A invariably constructible subtype becomes a supertype, and thus the enclosing type is never a root. |
| | 14883560 | 1373 | | case TypeParameter.TPVariance.Contra: { isRoot &= false; isLeaf &= r; break; } |
| | | 1374 | | } |
| | 17615360 | 1375 | | } |
| | 17615780 | 1376 | | } |
| | 8423640 | 1377 | | } else { |
| | 0 | 1378 | | isRoot = false; isLeaf = false; // don't know |
| | 0 | 1379 | | headIsRoot = false; headIsLeaf = false; |
| | 0 | 1380 | | } |
| | 1027317310 | 1381 | | } else if (t.IsBoolType || t.IsCharType || t.IsIntegerType || t.IsRealType || t.AsNewtype != null || t.IsBitVector |
| | 24622520 | 1382 | | isRoot = true; isLeaf = true; |
| | 24622520 | 1383 | | headIsRoot = true; headIsLeaf = true; |
| | 1006582410 | 1384 | | } else if (t is ArtificialType) { |
| | 0 | 1385 | | isRoot = false; isLeaf = false; |
| | 0 | 1386 | | headIsRoot = false; headIsLeaf = false; |
| | 994271150 | 1387 | | } else if (t is MapType) { // map, imap |
| | 0 | 1388 | | Contract.Assert(t.TypeArgs.Count == 2); |
| | 0 | 1389 | | DetermineRootLeaf(t.TypeArgs[0], out var r0, out _, out _, out _); |
| | 0 | 1390 | | DetermineRootLeaf(t.TypeArgs[1], out var r1, out _, out _, out _); |
| | 0 | 1391 | | isRoot = r0 & r1; isLeaf = r0 & r1; // map types are covariant in both type arguments |
| | 0 | 1392 | | headIsRoot = true; headIsLeaf = true; |
| | 998889800 | 1393 | | } else if (t is CollectionType) { // set, iset, multiset, seq |
| | 4618650 | 1394 | | Contract.Assert(t.TypeArgs.Count == 1); |
| | 4618650 | 1395 | | DetermineRootLeaf(t.TypeArgs[0], out isRoot, out isLeaf, out _, out _); // type is covariant is type argument |
| | 9237300 | 1396 | | headIsRoot = true; headIsLeaf = true; |
| | 994271150 | 1397 | | } else { |
| | 1979305000 | 1398 | | isRoot = false; isLeaf = false; // don't know |
| | 1979305000 | 1399 | | headIsRoot = false; headIsLeaf = false; |
| | 989652500 | 1400 | | } |
| | 1015011230 | 1401 | | } |
| | | 1402 | | |
| | 420 | 1403 | | int _recursionDepth = 0; |
| | 855770 | 1404 | | private bool AssignProxyAndHandleItsConstraints(TypeProxy proxy, Type t, bool keepConstraints = false) { |
| | | 1405 | | Contract.Requires(proxy != null); |
| | | 1406 | | Contract.Requires(proxy.T == null); |
| | | 1407 | | Contract.Requires(t != null); |
| | | 1408 | | Contract.Requires(!(t is TypeProxy)); |
| | | 1409 | | Contract.Requires(!(t is ArtificialType)); |
| | 855770 | 1410 | | if (_recursionDepth == 20000) { |
| | 0 | 1411 | | Contract.Assume(false); // possible infinite recursion |
| | 0 | 1412 | | } |
| | 855770 | 1413 | | _recursionDepth++; |
| | 855770 | 1414 | | var b = AssignProxyAndHandleItsConstraints_aux(proxy, t, keepConstraints); |
| | 855770 | 1415 | | _recursionDepth--; |
| | 855770 | 1416 | | return b; |
| | 855770 | 1417 | | } |
| | | 1418 | | /// <summary> |
| | | 1419 | | /// This method is called if "proxy" is an unassigned proxy and "t" is a type whose head symbol is known. |
| | | 1420 | | /// It always sets "proxy.T" to "t". |
| | | 1421 | | /// Then, it deals with the constraints of "proxy" as follows: |
| | | 1422 | | /// * If the constraint compares "t" with a non-proxy with a head comparable with that of "t", |
| | | 1423 | | /// then add constraints that the type arguments satisfy the desired subtyping constraint |
| | | 1424 | | /// * If the constraint compares "t" with a non-proxy with a head not comparable with that of "t", |
| | | 1425 | | /// then report an error |
| | | 1426 | | /// * If the constraint compares "t" with a proxy, then (depending on the constraint and "t") attempt |
| | | 1427 | | /// to recursively set it |
| | | 1428 | | /// After this process, the proxy's .Supertypes and .Subtypes lists of constraints are no longer needed. |
| | | 1429 | | /// If anything is found to be infeasible, "false" is returned (and the propagation may be interrupted); |
| | | 1430 | | /// otherwise, "true" is returned. |
| | | 1431 | | /// </summary> |
| | 855770 | 1432 | | private bool AssignProxyAndHandleItsConstraints_aux(TypeProxy proxy, Type t, bool keepConstraints = false) { |
| | | 1433 | | Contract.Requires(proxy != null); |
| | | 1434 | | Contract.Requires(proxy.T == null); |
| | | 1435 | | Contract.Requires(t != null); |
| | | 1436 | | Contract.Requires(!(t is TypeProxy)); |
| | | 1437 | | Contract.Requires(!(t is ArtificialType)); |
| | | 1438 | | |
| | 855770 | 1439 | | t = keepConstraints ? t.Normalize() : t.NormalizeExpand(); |
| | | 1440 | | // never violate the type constraint of a literal expression |
| | 855770 | 1441 | | var followedRequestedAssignment = true; |
| | 4991800 | 1442 | | foreach (var su in proxy.Supertypes) { |
| | 939590 | 1443 | | if (su is IntVarietiesSupertype) { |
| | 98570 | 1444 | | var fam = TypeProxy.GetFamily(t); |
| | 197140 | 1445 | | if (fam == TypeProxy.Family.IntLike || fam == TypeProxy.Family.BitVector || fam == TypeProxy.Family.Ordinal) { |
| | | 1446 | | // good, let's continue with the request to equate the proxy with t |
| | | 1447 | | // unless... |
| | 98570 | 1448 | | if (t != t.NormalizeExpand()) { |
| | | 1449 | | // force the type to be a base type |
| | 0 | 1450 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 1451 | | Options.OutputWriter.WriteLine("DEBUG: hijacking {0}.T := {1} to instead assign {2}", proxy, t, t.Normal |
| | 0 | 1452 | | } |
| | 0 | 1453 | | t = t.NormalizeExpand(); |
| | 0 | 1454 | | followedRequestedAssignment = false; |
| | 0 | 1455 | | } |
| | 98570 | 1456 | | } else { |
| | | 1457 | | // hijack the setting of proxy; to do that, we decide on a particular int variety now |
| | 0 | 1458 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 1459 | | Options.OutputWriter.WriteLine("DEBUG: hijacking {0}.T := {1} to instead assign {2}", proxy, t, Type.Int); |
| | 0 | 1460 | | } |
| | 0 | 1461 | | t = Type.Int; |
| | 0 | 1462 | | followedRequestedAssignment = false; |
| | 0 | 1463 | | } |
| | 98570 | 1464 | | break; |
| | 742450 | 1465 | | } else if (su is RealVarietiesSupertype) { |
| | 0 | 1466 | | if (TypeProxy.GetFamily(t) == TypeProxy.Family.RealLike) { |
| | | 1467 | | // good, let's continue with the request to equate the proxy with t |
| | | 1468 | | // unless... |
| | 0 | 1469 | | if (t != t.NormalizeExpand()) { |
| | | 1470 | | // force the type to be a base type |
| | 0 | 1471 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 1472 | | Options.OutputWriter.WriteLine("DEBUG: hijacking {0}.T := {1} to instead assign {2}", proxy, t, t.Normal |
| | 0 | 1473 | | } |
| | 0 | 1474 | | t = t.NormalizeExpand(); |
| | 0 | 1475 | | followedRequestedAssignment = false; |
| | 0 | 1476 | | } |
| | 0 | 1477 | | } else { |
| | | 1478 | | // hijack the setting of proxy; to do that, we decide on a particular real variety now |
| | 0 | 1479 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 1480 | | Options.OutputWriter.WriteLine("DEBUG: hijacking {0}.T := {1} to instead assign {2}", proxy, t, Type.Real) |
| | 0 | 1481 | | } |
| | 0 | 1482 | | t = Type.Real; |
| | 0 | 1483 | | followedRequestedAssignment = false; |
| | 0 | 1484 | | } |
| | 0 | 1485 | | break; |
| | | 1486 | | } |
| | 742450 | 1487 | | } |
| | | 1488 | | // set proxy.T right away, so that we can freely recurse without having to worry about infinite recursion |
| | 855770 | 1489 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 1490 | | Options.OutputWriter.WriteLine("DEBUG: setting proxy {0}.T := {1}", proxy, t); |
| | 0 | 1491 | | } |
| | 855770 | 1492 | | proxy.T = t; |
| | | 1493 | | |
| | | 1494 | | // check feasibility |
| | 855770 | 1495 | | DetermineRootLeaf(t, out var isRoot, out var isLeaf, out _, out _); |
| | | 1496 | | // propagate up |
| | 5168010 | 1497 | | foreach (var c in proxy.SupertypeConstraints) { |
| | 866900 | 1498 | | var u = keepConstraints ? c.Super.NormalizeExpandKeepConstraints() : c.Super.NormalizeExpand(); |
| | 1617560 | 1499 | | if (!(u is TypeProxy)) { |
| | 750660 | 1500 | | ImposeSubtypingConstraint(u, t, c.ErrMsg); |
| | 957300 | 1501 | | } else if (isRoot) { |
| | | 1502 | | // If t is a root, we might as well constrain u now. Otherwise, we'll wait until the .Subtype constraint of u |
| | 90400 | 1503 | | AssignProxyAndHandleItsConstraints((TypeProxy)u, t, keepConstraints); |
| | 90400 | 1504 | | } |
| | 866900 | 1505 | | } |
| | | 1506 | | // propagate down |
| | 5893470 | 1507 | | foreach (var c in proxy.SubtypeConstraints) { |
| | 1108720 | 1508 | | var u = keepConstraints ? c.Sub.NormalizeExpandKeepConstraints() : c.Sub.NormalizeExpand(); |
| | 1108720 | 1509 | | Contract.Assert(!TypeProxy.IsSupertypeOfLiteral(u)); // these should only appear among .Supertypes |
| | 2001120 | 1510 | | if (!(u is TypeProxy)) { |
| | 892400 | 1511 | | ImposeSubtypingConstraint(t, u, c.ErrMsg); |
| | 1291980 | 1512 | | } else if (isLeaf) { |
| | | 1513 | | // If t is a leaf (no pun intended), we might as well constrain u now. Otherwise, we'll wait until the .Super |
| | 183260 | 1514 | | AssignProxyAndHandleItsConstraints((TypeProxy)u, t, keepConstraints); |
| | 183260 | 1515 | | } |
| | 1108720 | 1516 | | } |
| | | 1517 | | |
| | 855770 | 1518 | | return followedRequestedAssignment; |
| | 855770 | 1519 | | } |
| | | 1520 | | |
| | | 1521 | | /// <summary> |
| | | 1522 | | /// Impose constraints that "sub" is a subtype of "super", returning "false" if this leads to an overconstrained sit |
| | | 1523 | | /// In most cases, "sub" being a subtype of "super" means that "sub" and "super" have the same head symbol and, ther |
| | | 1524 | | /// same number of type arguments. Depending on the polarities of the type parameters, the corresponding arguments |
| | | 1525 | | /// of "sub" and "super" must be in co-, in-, or contra-variant relationships to each other. |
| | | 1526 | | /// There are two ways "sub" can be a subtype of "super" without the two having the same head symbol. |
| | | 1527 | | /// One way is that "sub" is a subset type. In this case, the method starts by moving "sub" up toward "super". |
| | | 1528 | | /// The other way is that "super" is a trait (possibly |
| | | 1529 | | /// the trait "object"). By a current restriction in Dafny's type system, traits have no type parameters, so in thi |
| | | 1530 | | /// suffices to check that the head symbol of "super" is something that derives from "sub". |
| | | 1531 | | /// </summary> |
| | 2071290 | 1532 | | private bool ImposeSubtypingConstraint(Type super, Type sub, TypeConstraint.ErrorMsg errorMsg) { |
| | | 1533 | | Contract.Requires(super != null && !(super is TypeProxy)); |
| | | 1534 | | Contract.Requires(sub != null && !(sub is TypeProxy)); |
| | | 1535 | | Contract.Requires(errorMsg != null); |
| | 2071290 | 1536 | | super = super.NormalizeExpandKeepConstraints(); |
| | 2071290 | 1537 | | sub = sub.NormalizeExpandKeepConstraints(); |
| | 2071290 | 1538 | | List<int> polarities = ConstrainTypeHead_Recursive(super, ref sub); |
| | 2071290 | 1539 | | if (polarities == null) { |
| | 0 | 1540 | | errorMsg.FlagAsError(this); |
| | 0 | 1541 | | return false; |
| | | 1542 | | } |
| | 2071290 | 1543 | | bool keepConstraints = KeepConstraints(super, sub); |
| | 2071290 | 1544 | | var p = polarities.Count; |
| | 2071290 | 1545 | | Contract.Assert(p == super.TypeArgs.Count); // postcondition of ConstrainTypeHead |
| | 2071290 | 1546 | | Contract.Assert(p == 0 || sub.TypeArgs.Count == super.TypeArgs.Count); // postcondition of ConstrainTypeHead |
| | 8055360 | 1547 | | for (int i = 0; i < p; i++) { |
| | 1304260 | 1548 | | var pol = polarities[i]; |
| | 1304260 | 1549 | | var tp = p == 1 ? "" : " " + i; |
| | 1304260 | 1550 | | var errMsg = new TypeConstraint.ErrorMsgWithBase(errorMsg, |
| | 1304260 | 1551 | | pol < 0 ? "contravariant type parameter{0} would require {1} <: {2}" : |
| | 1304260 | 1552 | | pol > 0 ? "covariant type parameter{0} would require {2} <: {1}" : |
| | 1304260 | 1553 | | "non-variant type parameter{0} would require {1} = {2}", |
| | 1304260 | 1554 | | tp, super.TypeArgs[i], sub.TypeArgs[i]); |
| | 2605860 | 1555 | | if (pol >= 0) { |
| | 1301600 | 1556 | | if (!ConstrainSubtypeRelation(super.TypeArgs[i], sub.TypeArgs[i], errMsg, keepConstraints)) { |
| | 0 | 1557 | | return false; |
| | | 1558 | | } |
| | 1301600 | 1559 | | } |
| | 1306920 | 1560 | | if (pol <= 0) { |
| | 2660 | 1561 | | if (!ConstrainSubtypeRelation(sub.TypeArgs[i], super.TypeArgs[i], errMsg, keepConstraints)) { |
| | 0 | 1562 | | return false; |
| | | 1563 | | } |
| | 2660 | 1564 | | } |
| | 1304260 | 1565 | | } |
| | 2071290 | 1566 | | return true; |
| | 2071290 | 1567 | | } |
| | | 1568 | | |
| | | 1569 | | /// <summary> |
| | | 1570 | | /// This is a more liberal version of "ConstrainTypeHead" below. It is willing to move "sub" |
| | | 1571 | | /// upward toward its parents until it finds a head that matches "super", if any. |
| | | 1572 | | /// </summary> |
| | 2071670 | 1573 | | private static List<int> ConstrainTypeHead_Recursive(Type super, ref Type sub) { |
| | | 1574 | | Contract.Requires(super != null); |
| | | 1575 | | Contract.Requires(sub != null); |
| | | 1576 | | |
| | 2071670 | 1577 | | super = super.NormalizeExpandKeepConstraints(); |
| | 2071670 | 1578 | | sub = sub.NormalizeExpandKeepConstraints(); |
| | | 1579 | | |
| | 2071670 | 1580 | | var polarities = ConstrainTypeHead(super, sub); |
| | 4142960 | 1581 | | if (polarities != null) { |
| | 2071290 | 1582 | | return polarities; |
| | | 1583 | | } |
| | | 1584 | | |
| | 1900 | 1585 | | foreach (var subParentType in sub.ParentTypes()) { |
| | 380 | 1586 | | sub = subParentType; |
| | 380 | 1587 | | polarities = ConstrainTypeHead_Recursive(super, ref sub); |
| | 760 | 1588 | | if (polarities != null) { |
| | 380 | 1589 | | return polarities; |
| | | 1590 | | } |
| | 0 | 1591 | | } |
| | | 1592 | | |
| | 0 | 1593 | | return null; |
| | 2071670 | 1594 | | } |
| | | 1595 | | |
| | | 1596 | | /// <summary> |
| | | 1597 | | /// Determines if the head of "sub" can be a subtype of "super". |
| | | 1598 | | /// If this is not possible, null is returned. |
| | | 1599 | | /// If it is possible, return a list of polarities, one for each type argument of "sub". Polarities |
| | | 1600 | | /// indicate: |
| | | 1601 | | /// +1 co-variant |
| | | 1602 | | /// 0 invariant |
| | | 1603 | | /// -1 contra-variant |
| | | 1604 | | /// "sub" is of some type that can (in general) have type parameters. |
| | | 1605 | | /// See also note about Dafny's current type system in the description of method "ImposeSubtypingConstraint". |
| | | 1606 | | /// </summary> |
| | 2071670 | 1607 | | private static List<int> ConstrainTypeHead(Type super, Type sub) { |
| | | 1608 | | Contract.Requires(super != null && !(super is TypeProxy)); |
| | | 1609 | | Contract.Requires(sub != null && !(sub is TypeProxy)); |
| | 2267760 | 1610 | | if (super is IntVarietiesSupertype) { |
| | 196090 | 1611 | | var famSub = TypeProxy.GetFamily(sub); |
| | 392180 | 1612 | | if (famSub == TypeProxy.Family.IntLike || famSub == TypeProxy.Family.BitVector || famSub == TypeProxy.Family.Ord |
| | 196090 | 1613 | | return new List<int>(); |
| | 0 | 1614 | | } else { |
| | 0 | 1615 | | return null; |
| | | 1616 | | } |
| | 1875580 | 1617 | | } else if (super is RealVarietiesSupertype) { |
| | 0 | 1618 | | if (TypeProxy.GetFamily(sub) == TypeProxy.Family.RealLike || super.Equals(sub)) { |
| | 0 | 1619 | | return new List<int>(); |
| | 0 | 1620 | | } else { |
| | 0 | 1621 | | return null; |
| | | 1622 | | } |
| | | 1623 | | } |
| | 1875580 | 1624 | | switch (TypeProxy.GetFamily(super)) { |
| | | 1625 | | case TypeProxy.Family.Bool: |
| | | 1626 | | case TypeProxy.Family.Char: |
| | | 1627 | | case TypeProxy.Family.IntLike: |
| | | 1628 | | case TypeProxy.Family.RealLike: |
| | | 1629 | | case TypeProxy.Family.Ordinal: |
| | | 1630 | | case TypeProxy.Family.BitVector: |
| | 2071760 | 1631 | | if (super.Equals(sub)) { |
| | 1035880 | 1632 | | if (sub is UserDefinedType subUserDefinedType) { |
| | 0 | 1633 | | return subUserDefinedType.ResolvedClass.TypeArgs.ConvertAll(tp => TypeParameter.Direction(tp.Variance)); |
| | 1035880 | 1634 | | } else { |
| | 1035880 | 1635 | | return new List<int>(); |
| | | 1636 | | } |
| | 0 | 1637 | | } else { |
| | 0 | 1638 | | return null; |
| | | 1639 | | } |
| | | 1640 | | case TypeProxy.Family.ValueType: |
| | | 1641 | | case TypeProxy.Family.Ref: |
| | | 1642 | | case TypeProxy.Family.Opaque: |
| | 839700 | 1643 | | break; // more elaborate work below |
| | | 1644 | | case TypeProxy.Family.Unknown: |
| | 0 | 1645 | | return null; |
| | | 1646 | | default: |
| | 0 | 1647 | | Contract.Assert(false); // unexpected type (the precondition of ConstrainTypeHead says "no proxies") |
| | 0 | 1648 | | return null; // please compiler |
| | | 1649 | | } |
| | 933670 | 1650 | | if (super is SetType) { |
| | 93970 | 1651 | | var tt = (SetType)super; |
| | 93970 | 1652 | | var uu = sub as SetType; |
| | 93970 | 1653 | | return uu != null && tt.Finite == uu.Finite ? new List<int> { 1 } : null; |
| | 894230 | 1654 | | } else if (super is SeqType) { |
| | 148500 | 1655 | | return sub is SeqType ? new List<int> { 1 } : null; |
| | 690760 | 1656 | | } else if (super is MultiSetType) { |
| | 93530 | 1657 | | return sub is MultiSetType ? new List<int> { 1 } : null; |
| | 503700 | 1658 | | } else if (super is MapType) { |
| | 0 | 1659 | | var tt = (MapType)super; |
| | 0 | 1660 | | var uu = sub as MapType; |
| | 0 | 1661 | | return uu != null && tt.Finite == uu.Finite ? new List<int> { 1, 1 } : null; |
| | 503700 | 1662 | | } else if (super.IsObjectQ) { |
| | 0 | 1663 | | return sub.IsRefType ? new List<int>() : null; |
| | 503700 | 1664 | | } else { |
| | | 1665 | | // The only remaining cases are that "super" is a (co)datatype, abstract type, or non-object trait/class. |
| | | 1666 | | // In each of these cases, "super" is a UserDefinedType. |
| | 503700 | 1667 | | var udfSuper = (UserDefinedType)super; |
| | 503700 | 1668 | | var clSuper = udfSuper.ResolvedClass; |
| | 503700 | 1669 | | if (clSuper == null) { |
| | 0 | 1670 | | Contract.Assert(super.TypeArgs.Count == 0); |
| | 0 | 1671 | | if (super.IsTypeParameter) { |
| | | 1672 | | // we're looking at a type parameter |
| | 0 | 1673 | | return super.AsTypeParameter == sub.AsTypeParameter ? new List<int>() : null; |
| | 0 | 1674 | | } else { |
| | 0 | 1675 | | Contract.Assert(super.IsInternalTypeSynonym); |
| | 0 | 1676 | | return super.AsInternalTypeSynonym == sub.AsInternalTypeSynonym ? new List<int>() : null; |
| | | 1677 | | } |
| | | 1678 | | } |
| | 503700 | 1679 | | var udfSub = sub as UserDefinedType; |
| | 503700 | 1680 | | var clSub = udfSub == null ? null : udfSub.ResolvedClass; |
| | 503700 | 1681 | | if (clSub == null) { |
| | 0 | 1682 | | return null; |
| | 1007020 | 1683 | | } else if (clSuper == clSub) { |
| | | 1684 | | // good |
| | 503320 | 1685 | | var polarities = new List<int>(); |
| | 503320 | 1686 | | Contract.Assert(clSuper.TypeArgs.Count == udfSuper.TypeArgs.Count); |
| | 503320 | 1687 | | Contract.Assert(clSuper.TypeArgs.Count == udfSub.TypeArgs.Count); |
| | 4414740 | 1688 | | foreach (var tp in clSuper.TypeArgs) { |
| | 968260 | 1689 | | var polarity = TypeParameter.Direction(tp.Variance); |
| | 968260 | 1690 | | polarities.Add(polarity); |
| | 968260 | 1691 | | } |
| | | 1692 | | |
| | 503320 | 1693 | | return polarities; |
| | 380 | 1694 | | } else if (udfSub.IsRefType && super.IsObjectQ) { |
| | 0 | 1695 | | return new List<int>(); |
| | 380 | 1696 | | } else if (udfSub.IsNonNullRefType && super.IsObject) { |
| | 0 | 1697 | | return new List<int>(); |
| | 380 | 1698 | | } else { |
| | 380 | 1699 | | return null; |
| | | 1700 | | } |
| | | 1701 | | } |
| | 2071670 | 1702 | | } |
| | 2071290 | 1703 | | private static bool KeepConstraints(Type super, Type sub) { |
| | | 1704 | | Contract.Requires(super != null && !(super is TypeProxy)); |
| | | 1705 | | Contract.Requires(sub != null && !(sub is TypeProxy)); |
| | 2267380 | 1706 | | if (super is IntVarietiesSupertype) { |
| | 196090 | 1707 | | return false; |
| | 1875200 | 1708 | | } else if (super is RealVarietiesSupertype) { |
| | 0 | 1709 | | return false; |
| | | 1710 | | } |
| | 1875200 | 1711 | | switch (TypeProxy.GetFamily(super)) { |
| | | 1712 | | case TypeProxy.Family.Bool: |
| | | 1713 | | case TypeProxy.Family.Char: |
| | | 1714 | | case TypeProxy.Family.IntLike: |
| | | 1715 | | case TypeProxy.Family.RealLike: |
| | | 1716 | | case TypeProxy.Family.Ordinal: |
| | | 1717 | | case TypeProxy.Family.BitVector: |
| | 1035880 | 1718 | | return false; |
| | | 1719 | | case TypeProxy.Family.ValueType: |
| | | 1720 | | case TypeProxy.Family.Ref: |
| | | 1721 | | case TypeProxy.Family.Opaque: |
| | 839320 | 1722 | | break; // more elaborate work below |
| | | 1723 | | case TypeProxy.Family.Unknown: |
| | 0 | 1724 | | return false; |
| | | 1725 | | } |
| | 1175320 | 1726 | | if (super is SetType || super is SeqType || super is MultiSetType || super is MapType) { |
| | 336000 | 1727 | | return true; |
| | 503750 | 1728 | | } else if (super is ArrowType) { |
| | 430 | 1729 | | return false; |
| | 502890 | 1730 | | } else if (super.IsObjectQ) { |
| | 0 | 1731 | | return false; |
| | 502890 | 1732 | | } else { |
| | | 1733 | | // super is UserDefinedType |
| | 502890 | 1734 | | return true; |
| | | 1735 | | } |
| | 2071290 | 1736 | | } |
| | | 1737 | | |
| | 420 | 1738 | | public List<TypeConstraint> AllTypeConstraints = new List<TypeConstraint>(); |
| | 420 | 1739 | | public List<XConstraint> AllXConstraints = new List<XConstraint>(); |
| | | 1740 | | |
| | | 1741 | | public class XConstraint { |
| | | 1742 | | public readonly IToken tok; |
| | | 1743 | | public readonly string ConstraintName; |
| | | 1744 | | public readonly Type[] Types; |
| | | 1745 | | public readonly TypeConstraint.ErrorMsg errorMsg; |
| | 2335820 | 1746 | | public XConstraint(IToken tok, string constraintName, Type[] types, TypeConstraint.ErrorMsg errMsg) { |
| | | 1747 | | Contract.Requires(tok != null); |
| | | 1748 | | Contract.Requires(constraintName != null); |
| | | 1749 | | Contract.Requires(types != null); |
| | | 1750 | | Contract.Requires(errMsg != null); |
| | 1167910 | 1751 | | this.tok = tok; |
| | 1167910 | 1752 | | ConstraintName = constraintName; |
| | 1167910 | 1753 | | Types = types; |
| | 1167910 | 1754 | | errorMsg = errMsg; |
| | 1167910 | 1755 | | } |
| | | 1756 | | |
| | 0 | 1757 | | public override string ToString() { |
| | 0 | 1758 | | var s = ConstraintName + ":"; |
| | 0 | 1759 | | foreach (var t in Types) { |
| | 0 | 1760 | | s += " " + t; |
| | 0 | 1761 | | } |
| | 0 | 1762 | | return s; |
| | 0 | 1763 | | } |
| | | 1764 | | |
| | | 1765 | | /// <summary> |
| | | 1766 | | /// Tries to confirm the XConstraint. |
| | | 1767 | | /// If the XConstraint can be confirmed, or at least is plausible enough to have been converted into other type |
| | | 1768 | | /// constraints or more XConstraints, then "true" is returned and the out-parameters "convertedIntoOtherTypeConstr |
| | | 1769 | | /// and "moreXConstraints" are set to true accordingly. |
| | | 1770 | | /// If the XConstraint can be refuted, then an error message will be produced and "true" is returned (to indicate |
| | | 1771 | | /// that this XConstraint has finished serving its purpose). |
| | | 1772 | | /// If there's not enough information to confirm or refute the XConstraint, then "false" is returned. |
| | | 1773 | | /// </summary> |
| | 548351860 | 1774 | | public bool Confirm(Resolver resolver, bool fullstrength, out bool convertedIntoOtherTypeConstraints, out bool mor |
| | | 1775 | | Contract.Requires(resolver != null); |
| | 548351860 | 1776 | | convertedIntoOtherTypeConstraints = false; |
| | 548351860 | 1777 | | moreXConstraints = false; |
| | 548351860 | 1778 | | var t = Types[0].NormalizeExpand(); |
| | 1085480520 | 1779 | | if (t is TypeProxy) { |
| | 537128660 | 1780 | | switch (ConstraintName) { |
| | | 1781 | | case "Assignable": |
| | | 1782 | | case "Equatable": |
| | | 1783 | | case "EquatableArg": |
| | | 1784 | | case "Indexable": |
| | | 1785 | | case "Innable": |
| | | 1786 | | case "MultiIndexable": |
| | | 1787 | | case "IntOrORDINAL": |
| | | 1788 | | // have a go downstairs |
| | 502178590 | 1789 | | break; |
| | | 1790 | | default: |
| | 34950070 | 1791 | | return false; // there's not enough information to confirm or refute this XConstraint |
| | | 1792 | | } |
| | 502178590 | 1793 | | } |
| | | 1794 | | bool satisfied; |
| | 513401790 | 1795 | | switch (ConstraintName) { |
| | 511717500 | 1796 | | case "Assignable": { |
| | 511717500 | 1797 | | Contract.Assert(t == t.Normalize()); // it's already been normalized above |
| | 511717500 | 1798 | | var u = Types[1].NormalizeExpand(); |
| | 511717500 | 1799 | | if (CheckTypeInferenceVisitor.IsDetermined(t) && |
| | 511717500 | 1800 | | (fullstrength |
| | 511717500 | 1801 | | || !ProxyWithNoSubTypeConstraint(u, resolver) |
| | 511717500 | 1802 | | || (u is TypeProxy |
| | 511717500 | 1803 | | && Types[0].NormalizeExpandKeepConstraints() is var t0constrained |
| | 511717500 | 1804 | | && (t0constrained.IsNonNullRefType || t0constrained.AsSubsetType != null) |
| | 512163720 | 1805 | | && resolver.HasApplicableNullableRefTypeConstraint(new HashSet<TypeProxy>() { (TypeProxy)u })))) |
| | | 1806 | | // This is the best case. We convert Assignable(t, u) to the subtype constraint base(t) :> u. |
| | 446220 | 1807 | | if (CheckTypeInferenceVisitor.IsDetermined(u) && t.IsSubtypeOf(u, false, true) && t.IsRefType) { |
| | | 1808 | | // But we also allow cases where the rhs is a proper supertype of the lhs, and let the verifier |
| | | 1809 | | // determine whether the rhs is provably an instance of the lhs. |
| | 0 | 1810 | | resolver.ConstrainAssignable((NonProxyType)u, (NonProxyType)t, errorMsg, out moreXConstraints, fullstr |
| | 446220 | 1811 | | } else { |
| | 446220 | 1812 | | resolver.ConstrainAssignable((NonProxyType)t, u, errorMsg, out moreXConstraints, fullstrength); |
| | 446220 | 1813 | | } |
| | 446220 | 1814 | | convertedIntoOtherTypeConstraints = true; |
| | 446220 | 1815 | | return true; |
| | 511271280 | 1816 | | } else if (u.IsTypeParameter) { |
| | | 1817 | | // we need the constraint base(t) :> u, which for a type parameter t can happen iff t :> u |
| | 0 | 1818 | | resolver.ConstrainSubtypeRelation(t, u, errorMsg); |
| | 0 | 1819 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 1820 | | return true; |
| | 511599830 | 1821 | | } else if (Type.FromSameHead(t, u, out var tUp, out var uUp)) { |
| | 328550 | 1822 | | resolver.ConstrainAssignableTypeArgs(tUp, tUp.TypeArgs, uUp.TypeArgs, errorMsg, out moreXConstraints); |
| | 328550 | 1823 | | return true; |
| | 510942730 | 1824 | | } else if (fullstrength && t is NonProxyType) { |
| | | 1825 | | // We convert Assignable(t, u) to the subtype constraint base(t) :> u. |
| | 0 | 1826 | | resolver.ConstrainAssignable((NonProxyType)t, u, errorMsg, out moreXConstraints, fullstrength); |
| | 0 | 1827 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 1828 | | return true; |
| | 510942730 | 1829 | | } else if (fullstrength && u is NonProxyType) { |
| | | 1830 | | // We're willing to change "base(t) :> u" to the stronger constraint "t :> u" for the sake of making pro |
| | 0 | 1831 | | resolver.ConstrainSubtypeRelation(t, u, errorMsg); |
| | 0 | 1832 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 1833 | | return true; |
| | | 1834 | | } |
| | | 1835 | | // There's not enough information to say anything |
| | 510942730 | 1836 | | return false; |
| | | 1837 | | } |
| | | 1838 | | case "NumericType": |
| | 0 | 1839 | | satisfied = t.IsNumericBased(); |
| | 0 | 1840 | | break; |
| | | 1841 | | case "IntegerType": |
| | 420 | 1842 | | satisfied = t.IsNumericBased(Type.NumericPersuasion.Int); |
| | 420 | 1843 | | break; |
| | | 1844 | | case "IsBitvector": |
| | 0 | 1845 | | satisfied = t.IsBitVectorType; |
| | 0 | 1846 | | break; |
| | | 1847 | | case "IsRefType": |
| | 0 | 1848 | | satisfied = t.IsRefType; |
| | 0 | 1849 | | break; |
| | | 1850 | | case "IsNullableRefType": |
| | 840 | 1851 | | satisfied = t.IsRefType && !t.IsNonNullRefType; |
| | 840 | 1852 | | break; |
| | | 1853 | | case "Orderable_Lt": |
| | 22090 | 1854 | | satisfied = t.IsNumericBased() || t.IsBitVectorType || t.IsBigOrdinalType || t.IsCharType || t is SeqType || |
| | 22090 | 1855 | | break; |
| | | 1856 | | case "Orderable_Gt": |
| | 1300 | 1857 | | satisfied = t.IsNumericBased() || t.IsBitVectorType || t.IsBigOrdinalType || t.IsCharType || t is SetType || |
| | 1300 | 1858 | | break; |
| | 0 | 1859 | | case "RankOrderable": { |
| | 0 | 1860 | | var u = Types[1].NormalizeExpand(); |
| | 0 | 1861 | | if (u is TypeProxy) { |
| | 0 | 1862 | | return false; // not enough information |
| | | 1863 | | } |
| | 0 | 1864 | | satisfied = (t.IsIndDatatype || t.IsTypeParameter) && u.IsIndDatatype; |
| | 0 | 1865 | | break; |
| | | 1866 | | } |
| | | 1867 | | case "Plussable": |
| | 12740 | 1868 | | satisfied = t.IsNumericBased() || t.IsBitVectorType || t.IsBigOrdinalType || t.IsCharType || t is SeqType || |
| | 12740 | 1869 | | break; |
| | | 1870 | | case "Minusable": |
| | 1250 | 1871 | | satisfied = t.IsNumericBased() || t.IsBitVectorType || t.IsBigOrdinalType || t.IsCharType || t is SetType || |
| | 1250 | 1872 | | break; |
| | | 1873 | | case "Mullable": |
| | 830 | 1874 | | satisfied = t.IsNumericBased() || t.IsBitVectorType || t is SetType || t is MultiSetType; |
| | 830 | 1875 | | break; |
| | | 1876 | | case "IntOrORDINAL": |
| | 0 | 1877 | | if (!(t is TypeProxy)) { |
| | 0 | 1878 | | if (TernaryExpr.PrefixEqUsesNat) { |
| | 0 | 1879 | | satisfied = t.IsNumericBased(Type.NumericPersuasion.Int); |
| | 0 | 1880 | | } else { |
| | 0 | 1881 | | satisfied = t.IsNumericBased(Type.NumericPersuasion.Int) || t.IsBigOrdinalType; |
| | 0 | 1882 | | } |
| | 0 | 1883 | | } else if (fullstrength) { |
| | 0 | 1884 | | var proxy = (TypeProxy)t; |
| | 0 | 1885 | | if (TernaryExpr.PrefixEqUsesNat) { |
| | 0 | 1886 | | resolver.AssignProxyAndHandleItsConstraints(proxy, Type.Int); |
| | 0 | 1887 | | } else { |
| | | 1888 | | // let's choose ORDINAL over int |
| | 0 | 1889 | | resolver.AssignProxyAndHandleItsConstraints(proxy, Type.BigOrdinal); |
| | 0 | 1890 | | } |
| | 0 | 1891 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 1892 | | satisfied = true; |
| | 0 | 1893 | | } else { |
| | 0 | 1894 | | return false; |
| | | 1895 | | } |
| | 0 | 1896 | | break; |
| | | 1897 | | case "NumericOrBitvector": |
| | 34920 | 1898 | | satisfied = t.IsNumericBased() || t.IsBitVectorType; |
| | 34920 | 1899 | | break; |
| | | 1900 | | case "NumericOrBitvectorOrCharOrORDINAL": |
| | 940 | 1901 | | satisfied = t.IsNumericBased() || t.IsBitVectorType || t.IsCharType || t.IsBigOrdinalType; |
| | 940 | 1902 | | break; |
| | | 1903 | | case "IntLikeOrBitvector": |
| | 840 | 1904 | | satisfied = t.IsNumericBased(Type.NumericPersuasion.Int) || t.IsBitVectorType; |
| | 840 | 1905 | | break; |
| | | 1906 | | case "BooleanBits": |
| | 220 | 1907 | | satisfied = t.IsBoolType || t.IsBitVectorType; |
| | 220 | 1908 | | break; |
| | | 1909 | | case "Sizeable": |
| | 8960 | 1910 | | satisfied = (t is SetType && ((SetType)t).Finite) || t is MultiSetType || t is SeqType || (t is MapType && ( |
| | 8960 | 1911 | | break; |
| | | 1912 | | case "Disjointable": |
| | 1070 | 1913 | | satisfied = t is SetType || t is MultiSetType; |
| | 1070 | 1914 | | break; |
| | | 1915 | | case "MultiSetConvertible": |
| | 370 | 1916 | | satisfied = (t is SetType && ((SetType)t).Finite) || t is SeqType; |
| | 740 | 1917 | | if (satisfied) { |
| | 370 | 1918 | | Type elementType = ((CollectionType)t).Arg; |
| | 370 | 1919 | | var u = Types[1]; // note, it's okay if "u" is a TypeProxy |
| | 370 | 1920 | | var em = new TypeConstraint.ErrorMsgWithBase(errorMsg, "expecting element type {0} (got {1})", u, elementT |
| | 370 | 1921 | | resolver.ConstrainSubtypeRelation_Equal(elementType, u, em); |
| | 370 | 1922 | | convertedIntoOtherTypeConstraints = true; |
| | 370 | 1923 | | } |
| | 370 | 1924 | | break; |
| | | 1925 | | case "IsCoDatatype": |
| | 0 | 1926 | | satisfied = t.IsCoDatatype; |
| | 0 | 1927 | | break; |
| | | 1928 | | case "Indexable": |
| | 2190 | 1929 | | if (!(t is TypeProxy)) { |
| | 920 | 1930 | | satisfied = t is SeqType || t is MultiSetType || t is MapType || (t.IsArrayType && t.AsArrayType.Dims == 1 |
| | 1270 | 1931 | | } else { |
| | | 1932 | | // t is a proxy, but perhaps it stands for something between "object" and "array<?>". If so, we can add a |
| | | 1933 | | // that it does have the form "array<?>", since "object" would not be Indexable. |
| | 350 | 1934 | | var proxy = (TypeProxy)t; |
| | 350 | 1935 | | Type join = null; |
| | 700 | 1936 | | if (resolver.JoinOfAllSubtypes(proxy, ref join, new HashSet<TypeProxy>()) && join != null) { |
| | 350 | 1937 | | var headWithProxyArgs = Type.HeadWithProxyArgs(join); |
| | 350 | 1938 | | var tt = headWithProxyArgs.NormalizeExpand(); |
| | 350 | 1939 | | satisfied = tt is SeqType || tt is MultiSetType || tt is MapType || (tt.IsArrayType && tt.AsArrayType.Di |
| | 700 | 1940 | | if (satisfied) { |
| | 350 | 1941 | | resolver.AssignProxyAndHandleItsConstraints(proxy, headWithProxyArgs, true); |
| | 350 | 1942 | | convertedIntoOtherTypeConstraints = true; |
| | 350 | 1943 | | } |
| | 350 | 1944 | | } else { |
| | 0 | 1945 | | return false; // we can't determine the answer |
| | | 1946 | | } |
| | 350 | 1947 | | } |
| | 1270 | 1948 | | break; |
| | | 1949 | | case "MultiIndexable": |
| | 23520 | 1950 | | if (!(t is TypeProxy)) { |
| | 11760 | 1951 | | satisfied = t is SeqType || (t.IsArrayType && t.AsArrayType.Dims == 1); |
| | 11760 | 1952 | | } else { |
| | | 1953 | | // t is a proxy, but perhaps it stands for something between "object" and "array<?>". If so, we can add a |
| | | 1954 | | // that it does have the form "array<?>", since "object" would not be Indexable. |
| | 0 | 1955 | | var proxy = (TypeProxy)t; |
| | 0 | 1956 | | Type join = null; |
| | 0 | 1957 | | if (resolver.JoinOfAllSubtypes(proxy, ref join, new HashSet<TypeProxy>()) && join != null) { |
| | 0 | 1958 | | var headWithProxyArgs = Type.HeadWithProxyArgs(join); |
| | 0 | 1959 | | var tt = headWithProxyArgs.NormalizeExpand(); |
| | 0 | 1960 | | satisfied = tt is SeqType || (tt.IsArrayType && tt.AsArrayType.Dims == 1); |
| | 0 | 1961 | | if (satisfied) { |
| | 0 | 1962 | | resolver.AssignProxyAndHandleItsConstraints(proxy, headWithProxyArgs, true); |
| | 0 | 1963 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 1964 | | } |
| | 0 | 1965 | | } else { |
| | 0 | 1966 | | return false; // we can't determine the answer |
| | | 1967 | | } |
| | 0 | 1968 | | } |
| | 11760 | 1969 | | break; |
| | 550400 | 1970 | | case "Innable": { |
| | 550400 | 1971 | | var elementType = FindCollectionType(resolver.Options, t, true, new HashSet<TypeProxy>()) ?? FindCollectio |
| | 551590 | 1972 | | if (elementType != null) { |
| | 1190 | 1973 | | var u = Types[1]; // note, it's okay if "u" is a TypeProxy |
| | 1190 | 1974 | | resolver.AddXConstraint(this.tok, "Equatable", elementType, u, new TypeConstraint.ErrorMsgWithBase(error |
| | 1190 | 1975 | | moreXConstraints = true; |
| | 1190 | 1976 | | return true; |
| | | 1977 | | } |
| | 1098420 | 1978 | | if (t is TypeProxy) { |
| | 549210 | 1979 | | return false; // not enough information to do anything |
| | | 1980 | | } |
| | 0 | 1981 | | satisfied = false; |
| | 0 | 1982 | | break; |
| | | 1983 | | } |
| | 1260 | 1984 | | case "SeqUpdatable": { |
| | 1260 | 1985 | | var xcWithExprs = (XConstraintWithExprs)this; |
| | 1260 | 1986 | | var index = xcWithExprs.Exprs[0]; |
| | 1260 | 1987 | | var value = xcWithExprs.Exprs[1]; |
| | 2100 | 1988 | | if (t is SeqType) { |
| | 840 | 1989 | | var s = (SeqType)t; |
| | 840 | 1990 | | resolver.ConstrainToIntegerType(index, true, "sequence update requires integer- or bitvector-based index |
| | 840 | 1991 | | resolver.ConstrainSubtypeRelation(s.Arg, value.Type, value, "sequence update requires the value to have |
| | 1260 | 1992 | | } else if (t is MapType) { |
| | 0 | 1993 | | var s = (MapType)t; |
| | 0 | 1994 | | if (s.Finite) { |
| | 0 | 1995 | | resolver.ConstrainSubtypeRelation(s.Domain, index.Type, index, "map update requires domain element to |
| | 0 | 1996 | | resolver.ConstrainSubtypeRelation(s.Range, value.Type, value, "map update requires the value to have t |
| | 0 | 1997 | | } else { |
| | 0 | 1998 | | resolver.ConstrainSubtypeRelation(s.Domain, index.Type, index, "imap update requires domain element to |
| | 0 | 1999 | | resolver.ConstrainSubtypeRelation(s.Range, value.Type, value, "imap update requires the value to have |
| | 0 | 2000 | | } |
| | 840 | 2001 | | } else if (t is MultiSetType) { |
| | 420 | 2002 | | var s = (MultiSetType)t; |
| | 420 | 2003 | | resolver.ConstrainSubtypeRelation(s.Arg, index.Type, index, "multiset update requires domain element to |
| | 420 | 2004 | | resolver.ConstrainToIntegerType(value, false, "multiset update requires integer-based numeric value (got |
| | 420 | 2005 | | } else { |
| | 0 | 2006 | | satisfied = false; |
| | 0 | 2007 | | break; |
| | | 2008 | | } |
| | 1260 | 2009 | | convertedIntoOtherTypeConstraints = true; |
| | 1260 | 2010 | | return true; |
| | | 2011 | | } |
| | | 2012 | | case "ContainerIndex": |
| | | 2013 | | // The semantics of this XConstraint is that *if* the head is seq/array/map/multiset, then its element/domai |
| | | 2014 | | Type indexType; |
| | 35710 | 2015 | | if (t is SeqType || t.IsArrayType) { |
| | 17640 | 2016 | | resolver.ConstrainToIntegerType(errorMsg.Tok, Types[1], true, errorMsg); |
| | 17640 | 2017 | | convertedIntoOtherTypeConstraints = true; |
| | 17640 | 2018 | | return true; |
| | 430 | 2019 | | } else if (t is MapType) { |
| | 0 | 2020 | | indexType = ((MapType)t).Domain; |
| | 860 | 2021 | | } else if (t is MultiSetType) { |
| | 430 | 2022 | | indexType = ((MultiSetType)t).Arg; |
| | 430 | 2023 | | } else { |
| | | 2024 | | // some other head symbol; that's cool |
| | 0 | 2025 | | return true; |
| | | 2026 | | } |
| | | 2027 | | // note, it's okay if "Types[1]" is a TypeProxy |
| | 430 | 2028 | | resolver.ConstrainSubtypeRelation(indexType, Types[1], errorMsg); // use the same error message |
| | 430 | 2029 | | convertedIntoOtherTypeConstraints = true; |
| | 430 | 2030 | | return true; |
| | | 2031 | | case "ContainerResult": |
| | | 2032 | | // The semantics of this XConstraint is that *if* the head is seq/array/map/multiset, then the type of a sel |
| | | 2033 | | Type resultType; |
| | 25630 | 2034 | | if (t is SeqType) { |
| | 12600 | 2035 | | resultType = ((SeqType)t).Arg; |
| | 13030 | 2036 | | } else if (t.IsArrayType) { |
| | 0 | 2037 | | resultType = UserDefinedType.ArrayElementType(t); |
| | 430 | 2038 | | } else if (t is MapType) { |
| | 0 | 2039 | | resultType = ((MapType)t).Range; |
| | 860 | 2040 | | } else if (t is MultiSetType) { |
| | 430 | 2041 | | resultType = resolver.builtIns.Nat(); |
| | 430 | 2042 | | } else { |
| | | 2043 | | // some other head symbol; that's cool |
| | 0 | 2044 | | return true; |
| | | 2045 | | } |
| | | 2046 | | // note, it's okay if "Types[1]" is a TypeProxy |
| | 13030 | 2047 | | resolver.ConstrainSubtypeRelation(Types[1], resultType, errorMsg); |
| | 13030 | 2048 | | convertedIntoOtherTypeConstraints = true; |
| | 13030 | 2049 | | return true; |
| | 801230 | 2050 | | case "Equatable": { |
| | 801230 | 2051 | | t = Types[0].NormalizeExpandKeepConstraints(); |
| | 801230 | 2052 | | var u = Types[1].NormalizeExpandKeepConstraints(); |
| | 802040 | 2053 | | if (object.ReferenceEquals(t, u)) { |
| | 810 | 2054 | | return true; |
| | | 2055 | | } |
| | 1135180 | 2056 | | if (t is TypeProxy && u is TypeProxy) { |
| | 334760 | 2057 | | return false; // not enough information to do anything sensible |
| | 930150 | 2058 | | } else if (t is TypeProxy || u is TypeProxy) { |
| | | 2059 | | TypeProxy proxy; |
| | | 2060 | | Type other; |
| | 704060 | 2061 | | if (t is TypeProxy) { |
| | 239570 | 2062 | | proxy = (TypeProxy)t; |
| | 239570 | 2063 | | other = u; |
| | 464490 | 2064 | | } else { |
| | 224920 | 2065 | | proxy = (TypeProxy)u; |
| | 224920 | 2066 | | other = t; |
| | 224920 | 2067 | | } |
| | 465610 | 2068 | | if (other.IsNumericBased() || other.IsBitVectorType || other.IsBigOrdinalType) { |
| | 1120 | 2069 | | resolver.ConstrainSubtypeRelation(other.NormalizeExpand(), proxy, errorMsg, true); |
| | 1120 | 2070 | | convertedIntoOtherTypeConstraints = true; |
| | 1120 | 2071 | | return true; |
| | 464210 | 2072 | | } else if (fullstrength) { |
| | | 2073 | | // the following is rather aggressive |
| | 840 | 2074 | | if (Resolver.TypeConstraintsIncludeProxy(other, proxy)) { |
| | 0 | 2075 | | return false; |
| | 840 | 2076 | | } else { |
| | 1680 | 2077 | | if (other.IsRefType && resolver.HasApplicableNullableRefTypeConstraint_SubDirection(proxy)) { |
| | 840 | 2078 | | other = other.NormalizeExpand(); // shave off all constraints |
| | 840 | 2079 | | } |
| | 840 | 2080 | | satisfied = resolver.AssignProxyAndHandleItsConstraints(proxy, other, true); |
| | 840 | 2081 | | convertedIntoOtherTypeConstraints = true; |
| | 840 | 2082 | | break; |
| | | 2083 | | } |
| | 462530 | 2084 | | } else { |
| | 462530 | 2085 | | return false; // not enough information |
| | | 2086 | | } |
| | | 2087 | | } |
| | | 2088 | | |
| | 1170 | 2089 | | satisfied = Type.FromSameHead_Subtype(t, u, out var a, out var b); |
| | 2340 | 2090 | | if (satisfied) { |
| | 1170 | 2091 | | Contract.Assert(a.TypeArgs.Count == b.TypeArgs.Count); |
| | 1170 | 2092 | | var cl = a is UserDefinedType ? ((UserDefinedType)a).ResolvedClass : null; |
| | 5730 | 2093 | | for (int i = 0; i < a.TypeArgs.Count; i++) { |
| | 1130 | 2094 | | resolver.AllXConstraints.Add(new XConstraint_EquatableArg(tok, |
| | 1130 | 2095 | | a.TypeArgs[i], b.TypeArgs[i], |
| | 1130 | 2096 | | a is CollectionType || (cl != null && cl.TypeArgs[i].Variance != TypeParameter.TPVariance.Non), |
| | 1130 | 2097 | | a.IsRefType, |
| | 1130 | 2098 | | errorMsg)); |
| | 1130 | 2099 | | moreXConstraints = true; |
| | 1130 | 2100 | | } |
| | 1170 | 2101 | | } |
| | 1170 | 2102 | | break; |
| | | 2103 | | } |
| | 200480 | 2104 | | case "EquatableArg": { |
| | 200480 | 2105 | | t = Types[0].NormalizeExpandKeepConstraints(); |
| | 200480 | 2106 | | var u = Types[1].NormalizeExpandKeepConstraints(); |
| | 200480 | 2107 | | var moreExactThis = (XConstraint_EquatableArg)this; |
| | 304000 | 2108 | | if (t is TypeProxy && u is TypeProxy) { |
| | 103520 | 2109 | | return false; // not enough information to do anything sensible |
| | 191990 | 2110 | | } else if (t is TypeProxy || u is TypeProxy) { |
| | | 2111 | | TypeProxy proxy; |
| | | 2112 | | Type other; |
| | 128220 | 2113 | | if (t is TypeProxy) { |
| | 33190 | 2114 | | proxy = (TypeProxy)t; |
| | 33190 | 2115 | | other = u; |
| | 95030 | 2116 | | } else { |
| | 61840 | 2117 | | proxy = (TypeProxy)u; |
| | 61840 | 2118 | | other = t; |
| | 61840 | 2119 | | } |
| | 95210 | 2120 | | if (other.IsNumericBased() || other.IsBitVectorType || other.IsBigOrdinalType) { |
| | 180 | 2121 | | resolver.ConstrainSubtypeRelation(other.NormalizeExpand(), proxy, errorMsg, true); |
| | 180 | 2122 | | convertedIntoOtherTypeConstraints = true; |
| | 180 | 2123 | | return true; |
| | 94850 | 2124 | | } else if (fullstrength) { |
| | | 2125 | | // the following is rather aggressive |
| | 0 | 2126 | | if (Resolver.TypeConstraintsIncludeProxy(other, proxy)) { |
| | 0 | 2127 | | return false; |
| | 0 | 2128 | | } else { |
| | 0 | 2129 | | if (other.IsRefType && resolver.HasApplicableNullableRefTypeConstraint_SubDirection(proxy)) { |
| | 0 | 2130 | | other = other.NormalizeExpand(); // shave off all constraints |
| | 0 | 2131 | | } |
| | 0 | 2132 | | satisfied = resolver.AssignProxyAndHandleItsConstraints(proxy, other, true); |
| | 0 | 2133 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 2134 | | break; |
| | | 2135 | | } |
| | 94850 | 2136 | | } else { |
| | 94850 | 2137 | | return false; // not enough information |
| | | 2138 | | } |
| | | 2139 | | } |
| | 1930 | 2140 | | if (moreExactThis.TreatTypeParamAsWild && (t.IsTypeParameter || u.IsTypeParameter || t.IsAbstractType || u |
| | 0 | 2141 | | return true; |
| | 1930 | 2142 | | } else if (!moreExactThis.AllowSuperSub) { |
| | 0 | 2143 | | resolver.ConstrainSubtypeRelation_Equal(t, u, errorMsg); |
| | 0 | 2144 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 2145 | | return true; |
| | | 2146 | | } |
| | | 2147 | | |
| | | 2148 | | // okay if t<:u or u<:t (this makes type inference more manageable, though it is more liberal than one mig |
| | 1930 | 2149 | | satisfied = Type.FromSameHead_Subtype(t, u, out var a, out var b); |
| | 3860 | 2150 | | if (satisfied) { |
| | 1930 | 2151 | | Contract.Assert(a.TypeArgs.Count == b.TypeArgs.Count); |
| | 1930 | 2152 | | var cl = a is UserDefinedType ? ((UserDefinedType)a).ResolvedClass : null; |
| | 6800 | 2153 | | for (int i = 0; i < a.TypeArgs.Count; i++) { |
| | 980 | 2154 | | resolver.AllXConstraints.Add(new XConstraint_EquatableArg(tok, |
| | 980 | 2155 | | a.TypeArgs[i], b.TypeArgs[i], |
| | 980 | 2156 | | a is CollectionType || (cl != null && cl.TypeArgs[i].Variance != TypeParameter.TPVariance.Non), |
| | 980 | 2157 | | false, |
| | 980 | 2158 | | errorMsg)); |
| | 980 | 2159 | | moreXConstraints = true; |
| | 980 | 2160 | | } |
| | 1930 | 2161 | | } |
| | 1930 | 2162 | | break; |
| | | 2163 | | } |
| | 0 | 2164 | | case "Freshable": { |
| | 0 | 2165 | | var collType = t.AsCollectionType; |
| | 0 | 2166 | | if (collType is SetType || collType is SeqType) { |
| | 0 | 2167 | | t = collType.Arg.NormalizeExpand(); |
| | 0 | 2168 | | } |
| | 0 | 2169 | | if (t is TypeProxy) { |
| | 0 | 2170 | | return false; // there is not enough information |
| | | 2171 | | } |
| | 0 | 2172 | | satisfied = t.IsRefType; |
| | 0 | 2173 | | break; |
| | | 2174 | | } |
| | 0 | 2175 | | case "ModifiesFrame": { |
| | 0 | 2176 | | var u = Types[1].NormalizeExpand(); // eventual ref type |
| | 0 | 2177 | | var collType = t is MapType ? null : t.AsCollectionType; |
| | 0 | 2178 | | if (collType != null) { |
| | 0 | 2179 | | t = collType.Arg.NormalizeExpand(); |
| | 0 | 2180 | | } |
| | 0 | 2181 | | if (t is TypeProxy) { |
| | 0 | 2182 | | if (collType != null) { |
| | | 2183 | | // we know enough to convert into a subtyping constraint |
| | 0 | 2184 | | resolver.AddXConstraint(Token.NoToken/*bogus, but it seems this token would be used only when integers |
| | 0 | 2185 | | moreXConstraints = true; |
| | 0 | 2186 | | resolver.ConstrainSubtypeRelation_Equal(u, t, errorMsg); |
| | 0 | 2187 | | moreXConstraints = true; |
| | 0 | 2188 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 2189 | | return true; |
| | 0 | 2190 | | } else { |
| | 0 | 2191 | | return false; // there is not enough information |
| | | 2192 | | } |
| | | 2193 | | } |
| | 0 | 2194 | | if (t.IsRefType) { |
| | 0 | 2195 | | resolver.ConstrainSubtypeRelation_Equal(u, t, errorMsg); |
| | 0 | 2196 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 2197 | | return true; |
| | | 2198 | | } |
| | 0 | 2199 | | satisfied = false; |
| | 0 | 2200 | | break; |
| | | 2201 | | } |
| | 0 | 2202 | | case "ReadsFrame": { |
| | 0 | 2203 | | var u = Types[1].NormalizeExpand(); // eventual ref type |
| | 0 | 2204 | | var arrTy = t.AsArrowType; |
| | 0 | 2205 | | if (arrTy != null) { |
| | 0 | 2206 | | t = arrTy.Result.NormalizeExpand(); |
| | 0 | 2207 | | } |
| | 0 | 2208 | | var collType = t is MapType ? null : t.AsCollectionType; |
| | 0 | 2209 | | if (collType != null) { |
| | 0 | 2210 | | t = collType.Arg.NormalizeExpand(); |
| | 0 | 2211 | | } |
| | 0 | 2212 | | if (t is TypeProxy) { |
| | 0 | 2213 | | if (collType != null) { |
| | | 2214 | | // we know enough to convert into a subtyping constraint |
| | 0 | 2215 | | resolver.AddXConstraint(Token.NoToken/*bogus, but it seems this token would be used only when integers |
| | 0 | 2216 | | resolver.ConstrainSubtypeRelation_Equal(u, t, errorMsg); |
| | 0 | 2217 | | moreXConstraints = true; |
| | 0 | 2218 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 2219 | | return true; |
| | 0 | 2220 | | } else { |
| | 0 | 2221 | | return false; // there is not enough information |
| | | 2222 | | } |
| | | 2223 | | } |
| | 0 | 2224 | | if (t.IsRefType && (arrTy == null || collType != null)) { |
| | 0 | 2225 | | resolver.ConstrainSubtypeRelation_Equal(u, t, errorMsg); |
| | 0 | 2226 | | convertedIntoOtherTypeConstraints = true; |
| | 0 | 2227 | | return true; |
| | | 2228 | | } |
| | 0 | 2229 | | satisfied = false; |
| | 0 | 2230 | | break; |
| | | 2231 | | } |
| | | 2232 | | default: |
| | 0 | 2233 | | Contract.Assume(false); // unknown XConstraint |
| | 0 | 2234 | | return false; // to please the compiler |
| | | 2235 | | } |
| | 103760 | 2236 | | if (!satisfied) { |
| | 0 | 2237 | | errorMsg.FlagAsError(resolver); |
| | 0 | 2238 | | } |
| | 103760 | 2239 | | return true; // the XConstraint has served its purpose |
| | 548351860 | 2240 | | } |
| | | 2241 | | |
| | 7738650 | 2242 | | public bool ProxyWithNoSubTypeConstraint(Type u, Resolver resolver) { |
| | | 2243 | | Contract.Requires(u != null); |
| | | 2244 | | Contract.Requires(resolver != null); |
| | 7738650 | 2245 | | var proxy = u as TypeProxy; |
| | 15079750 | 2246 | | if (proxy != null) { |
| | 7341890 | 2247 | | if (proxy.SubtypeConstraints.Any()) { |
| | 790 | 2248 | | return false; |
| | | 2249 | | } |
| | 1161496648 | 2250 | | foreach (var xc in resolver.AllXConstraints) { |
| | -1051766686 | 2251 | | if (xc.ConstraintName == "Assignable" && xc.Types[0] == proxy) { |
| | 47880 | 2252 | | return false; |
| | | 2253 | | } |
| | -1051862446 | 2254 | | } |
| | 7292430 | 2255 | | return true; |
| | | 2256 | | } |
| | 397550 | 2257 | | return false; |
| | 7738650 | 2258 | | } |
| | | 2259 | | |
| | 0 | 2260 | | internal bool CouldBeAnything() { |
| | 0 | 2261 | | return Types.All(t => t.NormalizeExpand() is TypeProxy); |
| | 0 | 2262 | | } |
| | | 2263 | | |
| | | 2264 | | /// <summary> |
| | | 2265 | | /// If "t" or any type among its transitive sub/super-types (depending on "towardsSub") |
| | | 2266 | | /// is a collection type, then returns the element/domain type of that collection. |
| | | 2267 | | /// Otherwise, returns null. |
| | | 2268 | | /// </summary> |
| | 1438360 | 2269 | | Type FindCollectionType(DafnyOptions options, Type t, bool towardsSub, ISet<TypeProxy> visited) { |
| | | 2270 | | Contract.Requires(t != null); |
| | | 2271 | | Contract.Requires(visited != null); |
| | 1438360 | 2272 | | t = t.NormalizeExpand(); |
| | 1438360 | 2273 | | if (options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 2274 | | options.OutputWriter.WriteLine("DEBUG: FindCollectionType({0}, {1})", t, towardsSub ? "sub" : "super"); |
| | 0 | 2275 | | } |
| | 1439550 | 2276 | | if (t is CollectionType) { |
| | 1190 | 2277 | | if (options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 2278 | | options.OutputWriter.WriteLine("DEBUG: FindCollectionType({0}) = {1}", t, ((CollectionType)t).Arg); |
| | 0 | 2279 | | } |
| | 1190 | 2280 | | return ((CollectionType)t).Arg; |
| | | 2281 | | } |
| | 1437170 | 2282 | | var proxy = t as TypeProxy; |
| | 1619830 | 2283 | | if (proxy == null || visited.Contains(proxy)) { |
| | 182660 | 2284 | | return null; |
| | | 2285 | | } |
| | 1254510 | 2286 | | visited.Add(proxy); |
| | 4779260 | 2287 | | foreach (var sub in towardsSub ? proxy.Subtypes : proxy.Supertypes) { |
| | 338620 | 2288 | | var e = FindCollectionType(options, sub, towardsSub, visited); |
| | 338750 | 2289 | | if (e != null) { |
| | 130 | 2290 | | return e; |
| | | 2291 | | } |
| | 338490 | 2292 | | } |
| | 1254380 | 2293 | | return null; |
| | 1438360 | 2294 | | } |
| | | 2295 | | } |
| | | 2296 | | |
| | | 2297 | | public class XConstraintWithExprs : XConstraint { |
| | | 2298 | | public readonly Expression[] Exprs; |
| | | 2299 | | public XConstraintWithExprs(IToken tok, string constraintName, Type[] types, Expression[] exprs, TypeConstraint.Er |
| | 2520 | 2300 | | : base(tok, constraintName, types, errMsg) { |
| | | 2301 | | Contract.Requires(tok != null); |
| | | 2302 | | Contract.Requires(constraintName != null); |
| | | 2303 | | Contract.Requires(types != null); |
| | | 2304 | | Contract.Requires(exprs != null); |
| | | 2305 | | Contract.Requires(errMsg != null); |
| | 1260 | 2306 | | this.Exprs = exprs; |
| | 1260 | 2307 | | } |
| | | 2308 | | } |
| | | 2309 | | |
| | | 2310 | | public class XConstraint_EquatableArg : XConstraint { |
| | | 2311 | | public bool AllowSuperSub; |
| | | 2312 | | public bool TreatTypeParamAsWild; |
| | | 2313 | | public XConstraint_EquatableArg(IToken tok, Type a, Type b, bool allowSuperSub, bool treatTypeParamAsWild, TypeCon |
| | 4220 | 2314 | | : base(tok, "EquatableArg", new Type[] { a, b }, errMsg) { |
| | | 2315 | | Contract.Requires(tok != null); |
| | | 2316 | | Contract.Requires(a != null); |
| | | 2317 | | Contract.Requires(b != null); |
| | | 2318 | | Contract.Requires(errMsg != null); |
| | 2110 | 2319 | | AllowSuperSub = allowSuperSub; |
| | 2110 | 2320 | | TreatTypeParamAsWild = treatTypeParamAsWild; |
| | 2110 | 2321 | | } |
| | | 2322 | | } |
| | | 2323 | | |
| | | 2324 | | /// <summary> |
| | | 2325 | | /// Solves or simplifies as many type constraints as possible. |
| | | 2326 | | /// If "allowDecisions" is "false", then no decisions, only determined inferences, are made; this mode is |
| | | 2327 | | /// appropriate for the partial solving that's done before a member lookup. |
| | | 2328 | | /// </summary> |
| | 102220 | 2329 | | public void PartiallySolveTypeConstraints(bool allowDecisions) { |
| | 102220 | 2330 | | int state = 0; |
| | 3441820 | 2331 | | while (true) { |
| | 1750910 | 2332 | | if (2 <= state && !allowDecisions) { |
| | | 2333 | | // time to say goodnight to Napoli |
| | 30000 | 2334 | | return; |
| | 1763130 | 2335 | | } else if (AllTypeConstraints.Count == 0 && AllXConstraints.Count == 0) { |
| | | 2336 | | // we're done |
| | 72220 | 2337 | | return; |
| | | 2338 | | } |
| | | 2339 | | |
| | 1618690 | 2340 | | var anyNewConstraints = false; |
| | 1618690 | 2341 | | var fullStrength = false; |
| | | 2342 | | // Process subtyping constraints |
| | 1618690 | 2343 | | PrintTypeConstraintState(220 + 2 * state); |
| | 1618690 | 2344 | | switch (state) { |
| | 722970 | 2345 | | case 0: { |
| | 722970 | 2346 | | var allTypeConstraints = AllTypeConstraints; |
| | 722970 | 2347 | | AllTypeConstraints = new List<TypeConstraint>(); |
| | 722970 | 2348 | | var processed = new HashSet<TypeConstraint>(); |
| | 1159988490 | 2349 | | foreach (var c in allTypeConstraints) { |
| | 385939860 | 2350 | | ProcessOneSubtypingConstraintAndItsSubs(c, processed, fullStrength, ref anyNewConstraints); |
| | 385939860 | 2351 | | } |
| | | 2352 | | |
| | 722970 | 2353 | | allTypeConstraints = new List<TypeConstraint>(AllTypeConstraints); // copy the list |
| | 1154360070 | 2354 | | foreach (var c in allTypeConstraints) { |
| | 384063720 | 2355 | | var super = c.Super.NormalizeExpand() as TypeProxy; |
| | 384094390 | 2356 | | if (AssignKnownEnd(super, true, fullStrength)) { |
| | 30670 | 2357 | | anyNewConstraints = true; |
| | 384063720 | 2358 | | } else if (super != null && fullStrength && AssignKnownEndsFullstrength(super)) { // KRML: is this used |
| | 0 | 2359 | | anyNewConstraints = true; |
| | 0 | 2360 | | } |
| | 384063720 | 2361 | | } |
| | 722970 | 2362 | | } |
| | 722970 | 2363 | | break; |
| | | 2364 | | |
| | 570400 | 2365 | | case 1: { |
| | | 2366 | | // Process XConstraints |
| | | 2367 | | // confirm as many XConstraints as possible, setting "anyNewConstraints" to "true" if the confirmation |
| | | 2368 | | // of an XConstraint gives rise to new constraints to be handled in the loop above |
| | | 2369 | | bool generatedMoreXConstraints; |
| | 813280 | 2370 | | do { |
| | 813280 | 2371 | | generatedMoreXConstraints = false; |
| | 813280 | 2372 | | var allXConstraints = AllXConstraints; |
| | 813280 | 2373 | | AllXConstraints = new List<XConstraint>(); |
| | 1647492900 | 2374 | | foreach (var xc in allXConstraints) { |
| | 549264370 | 2375 | | if (xc.Confirm(this, fullStrength, out var convertedIntoOtherTypeConstraints, out var moreXConstraints |
| | 1393950 | 2376 | | if (convertedIntoOtherTypeConstraints) { |
| | 480600 | 2377 | | anyNewConstraints = true; |
| | 913350 | 2378 | | } else { |
| | 432750 | 2379 | | generatedMoreXConstraints = true; |
| | 432750 | 2380 | | } |
| | 1062400 | 2381 | | if (moreXConstraints) { |
| | 149050 | 2382 | | generatedMoreXConstraints = true; |
| | 149050 | 2383 | | } |
| | 548351020 | 2384 | | } else { |
| | 547437670 | 2385 | | AllXConstraints.Add(xc); |
| | 547437670 | 2386 | | } |
| | 548351020 | 2387 | | } |
| | 1626560 | 2388 | | } while (generatedMoreXConstraints); |
| | 570400 | 2389 | | } |
| | 570400 | 2390 | | break; |
| | | 2391 | | |
| | 286560 | 2392 | | case 2: { |
| | 191394330 | 2393 | | var assignables = AllXConstraints.Where(xc => xc.ConstraintName == "Assignable").ToList(); |
| | 286560 | 2394 | | var postponeForNow = new HashSet<TypeProxy>(); |
| | 496395690 | 2395 | | foreach (var constraint in AllTypeConstraints) { |
| | 165178670 | 2396 | | var lhs = constraint.Super.NormalizeExpandKeepConstraints() as NonProxyType; |
| | 210708790 | 2397 | | if (lhs != null) { |
| | 263099670 | 2398 | | foreach (var ta in lhs.TypeArgs) { |
| | 42169770 | 2399 | | AddAllProxies(ta, postponeForNow); |
| | 42169770 | 2400 | | } |
| | 45530120 | 2401 | | } |
| | 165178670 | 2402 | | } |
| | 496395690 | 2403 | | foreach (var constraint in AllTypeConstraints) { |
| | 165178670 | 2404 | | var lhs = constraint.Super.Normalize() as TypeProxy; |
| | 234554070 | 2405 | | if (lhs != null && !postponeForNow.Contains(lhs)) { |
| | -1250989081 | 2406 | | var rhss = assignables.Where(xc => xc.Types[0].Normalize() == lhs).Select(xc => xc.Types[1]).ToList(); |
| | 69379710 | 2407 | | if (ProcessAssignable(lhs, rhss)) { |
| | 4310 | 2408 | | anyNewConstraints = true; // next time around the big loop, start with state 0 again |
| | 4310 | 2409 | | } |
| | 69375400 | 2410 | | } |
| | 165178670 | 2411 | | } |
| | 196848160 | 2412 | | foreach (var assignable in assignables) { |
| | 65417920 | 2413 | | var lhs = assignable.Types[0].Normalize() as TypeProxy; |
| | 128751810 | 2414 | | if (lhs != null && !postponeForNow.Contains(lhs)) { |
| | 1073475972 | 2415 | | var rhss = assignables.Where(xc => xc.Types[0].Normalize() == lhs).Select(xc => xc.Types[1]).ToList(); |
| | 63599170 | 2416 | | if (ProcessAssignable(lhs, rhss)) { |
| | 265280 | 2417 | | anyNewConstraints = true; // next time around the big loop, start with state 0 again |
| | | 2418 | | // process only one Assignable constraint in this way |
| | 265280 | 2419 | | break; |
| | | 2420 | | } |
| | 63068610 | 2421 | | } |
| | 65152640 | 2422 | | } |
| | 286560 | 2423 | | } |
| | 286560 | 2424 | | break; |
| | | 2425 | | |
| | | 2426 | | case 3: |
| | 20610 | 2427 | | anyNewConstraints = ConvertAssignableToSubtypeConstraints(null); |
| | 20610 | 2428 | | break; |
| | | 2429 | | |
| | 13750 | 2430 | | case 4: { |
| | 13750 | 2431 | | var allTC = AllTypeConstraints; |
| | 13750 | 2432 | | AllTypeConstraints = new List<TypeConstraint>(); |
| | 13750 | 2433 | | var proxyProcessed = new HashSet<TypeProxy>(); |
| | 1725450 | 2434 | | foreach (var c in allTC) { |
| | 561400 | 2435 | | ProcessFullStrength_SubDirection(c.Super, proxyProcessed, ref anyNewConstraints); |
| | 561400 | 2436 | | } |
| | 764820 | 2437 | | foreach (var xc in AllXConstraints) { |
| | 407350 | 2438 | | if (xc.ConstraintName == "Assignable") { |
| | 166160 | 2439 | | ProcessFullStrength_SubDirection(xc.Types[0], proxyProcessed, ref anyNewConstraints); |
| | 166160 | 2440 | | } |
| | 241190 | 2441 | | } |
| | 27350 | 2442 | | if (!anyNewConstraints) { |
| | | 2443 | | // only do super-direction if sub-direction had no effect |
| | 13600 | 2444 | | proxyProcessed = new HashSet<TypeProxy>(); |
| | 1585170 | 2445 | | foreach (var c in allTC) { |
| | 514790 | 2446 | | ProcessFullStrength_SuperDirection(c.Sub, proxyProcessed, ref anyNewConstraints); |
| | 514790 | 2447 | | } |
| | 713310 | 2448 | | foreach (var xc in AllXConstraints) { |
| | 378140 | 2449 | | if (xc.ConstraintName == "Assignable") { |
| | 153970 | 2450 | | ProcessFullStrength_SuperDirection(xc.Types[1], proxyProcessed, ref anyNewConstraints); |
| | 153970 | 2451 | | } |
| | 224170 | 2452 | | } |
| | 13600 | 2453 | | } |
| | 13750 | 2454 | | AllTypeConstraints.AddRange(allTC); |
| | 13750 | 2455 | | } |
| | 13750 | 2456 | | break; |
| | | 2457 | | |
| | 3560 | 2458 | | case 5: { |
| | | 2459 | | // Process default numeric types |
| | 3560 | 2460 | | var allTypeConstraints = AllTypeConstraints; |
| | 3560 | 2461 | | AllTypeConstraints = new List<TypeConstraint>(); |
| | 227790 | 2462 | | foreach (var c in allTypeConstraints) { |
| | 92740 | 2463 | | if (c.Super is ArtificialType) { |
| | 20370 | 2464 | | var proxy = c.Sub.NormalizeExpand() as TypeProxy; |
| | 40340 | 2465 | | if (proxy != null) { |
| | 19970 | 2466 | | AssignProxyAndHandleItsConstraints(proxy, c.Super is IntVarietiesSupertype ? (Type)Type.Int : Type.R |
| | 19970 | 2467 | | anyNewConstraints = true; |
| | 19970 | 2468 | | continue; |
| | | 2469 | | } |
| | 400 | 2470 | | } |
| | 52400 | 2471 | | AllTypeConstraints.Add(c); |
| | 52400 | 2472 | | } |
| | 3560 | 2473 | | } |
| | 3560 | 2474 | | break; |
| | | 2475 | | |
| | 840 | 2476 | | case 6: { |
| | 840 | 2477 | | fullStrength = true; |
| | | 2478 | | bool generatedMoreXConstraints; |
| | 840 | 2479 | | do { |
| | 840 | 2480 | | generatedMoreXConstraints = false; |
| | 840 | 2481 | | var allXConstraints = AllXConstraints; |
| | 840 | 2482 | | AllXConstraints = new List<XConstraint>(); |
| | 7560 | 2483 | | foreach (var xc in allXConstraints) { |
| | 2520 | 2484 | | if ((xc.ConstraintName == "Equatable" || xc.ConstraintName == "EquatableArg") && xc.Confirm(this, full |
| | 1680 | 2485 | | if (convertedIntoOtherTypeConstraints) { |
| | 840 | 2486 | | anyNewConstraints = true; |
| | 840 | 2487 | | } else { |
| | 0 | 2488 | | generatedMoreXConstraints = true; |
| | 0 | 2489 | | } |
| | 840 | 2490 | | if (moreXConstraints) { |
| | 0 | 2491 | | generatedMoreXConstraints = true; |
| | 0 | 2492 | | } |
| | 1680 | 2493 | | } else { |
| | 840 | 2494 | | AllXConstraints.Add(xc); |
| | 840 | 2495 | | } |
| | 1680 | 2496 | | } |
| | 1680 | 2497 | | } while (generatedMoreXConstraints); |
| | 840 | 2498 | | } |
| | 840 | 2499 | | break; |
| | | 2500 | | |
| | 0 | 2501 | | case 7: { |
| | | 2502 | | // Process default reference types |
| | 0 | 2503 | | var allXConstraints = AllXConstraints; |
| | 0 | 2504 | | AllXConstraints = new List<XConstraint>(); |
| | 0 | 2505 | | foreach (var xc in allXConstraints) { |
| | 0 | 2506 | | if (xc.ConstraintName == "IsRefType" || xc.ConstraintName == "IsNullableRefType") { |
| | 0 | 2507 | | var proxy = xc.Types[0].Normalize() as TypeProxy; // before we started processing default types, this |
| | 0 | 2508 | | if (proxy != null) { |
| | 0 | 2509 | | AssignProxyAndHandleItsConstraints(proxy, builtIns.ObjectQ()); |
| | 0 | 2510 | | anyNewConstraints = true; |
| | 0 | 2511 | | continue; |
| | | 2512 | | } |
| | 0 | 2513 | | } |
| | 0 | 2514 | | AllXConstraints.Add(xc); |
| | 0 | 2515 | | } |
| | 0 | 2516 | | } |
| | 0 | 2517 | | break; |
| | | 2518 | | |
| | 0 | 2519 | | case 8: fullStrength = true; goto case 0; |
| | 0 | 2520 | | case 9: fullStrength = true; goto case 1; |
| | | 2521 | | |
| | 0 | 2522 | | case 10: { |
| | | 2523 | | // Finally, collapse constraints involving only proxies, which will have the effect of trading some type e |
| | | 2524 | | // messages for type-underspecification messages. |
| | 0 | 2525 | | var allTypeConstraints = AllTypeConstraints; |
| | 0 | 2526 | | AllTypeConstraints = new List<TypeConstraint>(); |
| | 0 | 2527 | | foreach (var c in allTypeConstraints) { |
| | 0 | 2528 | | var super = c.Super.NormalizeExpand(); |
| | 0 | 2529 | | var sub = c.Sub.NormalizeExpand(); |
| | 0 | 2530 | | if (super == sub) { |
| | 0 | 2531 | | continue; |
| | 0 | 2532 | | } else if (super is TypeProxy && sub is TypeProxy) { |
| | 0 | 2533 | | var proxy = (TypeProxy)super; |
| | 0 | 2534 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 2535 | | Options.OutputWriter.WriteLine("DEBUG: (merge in PartiallySolve) assigning proxy {0}.T := {1}", prox |
| | 0 | 2536 | | } |
| | 0 | 2537 | | proxy.T = sub; |
| | 0 | 2538 | | anyNewConstraints = true; // signal a change in the constraints |
| | 0 | 2539 | | continue; |
| | | 2540 | | } |
| | 0 | 2541 | | AllTypeConstraints.Add(c); |
| | 0 | 2542 | | } |
| | 0 | 2543 | | } |
| | 0 | 2544 | | break; |
| | | 2545 | | |
| | 0 | 2546 | | case 11: { |
| | | 2547 | | // Last resort decisions. Sometimes get here even with some 'obvious' |
| | | 2548 | | // inferences. Before this case was added, the type inference returned with |
| | | 2549 | | // failure, so this is a conservative addition, and could be made more |
| | | 2550 | | // capable. |
| | 0 | 2551 | | if (!allowDecisions) { |
| | 0 | 2552 | | break; |
| | | 2553 | | } |
| | | 2554 | | |
| | 0 | 2555 | | foreach (var c in AllXConstraints) { |
| | 0 | 2556 | | if (c.ConstraintName == "EquatableArg") { |
| | 0 | 2557 | | ConstrainSubtypeRelation_Equal(c.Types[0], c.Types[1], c.errorMsg); |
| | 0 | 2558 | | anyNewConstraints = true; |
| | 0 | 2559 | | AllXConstraints.Remove(c); |
| | 0 | 2560 | | break; |
| | | 2561 | | } |
| | 0 | 2562 | | } |
| | 0 | 2563 | | if (anyNewConstraints) { |
| | 0 | 2564 | | break; |
| | | 2565 | | } |
| | | 2566 | | |
| | 0 | 2567 | | TypeConstraint.ErrorMsg oneSuperErrorMsg = null; |
| | 0 | 2568 | | TypeConstraint.ErrorMsg oneSubErrorMsg = null; |
| | 0 | 2569 | | var ss = new HashSet<Type>(); |
| | 0 | 2570 | | foreach (var c in AllTypeConstraints) { |
| | 0 | 2571 | | var super = c.Super.NormalizeExpand(); |
| | 0 | 2572 | | var sub = c.Sub.NormalizeExpand(); |
| | 0 | 2573 | | if (super is TypeProxy && !ss.Contains(super)) { |
| | 0 | 2574 | | ss.Add(super); |
| | 0 | 2575 | | } |
| | 0 | 2576 | | if (sub is TypeProxy && !ss.Contains(sub)) { |
| | 0 | 2577 | | ss.Add(sub); |
| | 0 | 2578 | | } |
| | 0 | 2579 | | } |
| | | 2580 | | |
| | 0 | 2581 | | foreach (var t in ss) { |
| | 0 | 2582 | | var lowers = new HashSet<Type>(); |
| | 0 | 2583 | | var uppers = new HashSet<Type>(); |
| | 0 | 2584 | | foreach (var c in AllTypeConstraints) { |
| | 0 | 2585 | | var super = c.Super.NormalizeExpand(); |
| | 0 | 2586 | | var sub = c.Sub.NormalizeExpand(); |
| | 0 | 2587 | | if (t.Equals(super)) { |
| | 0 | 2588 | | lowers.Add(sub); |
| | 0 | 2589 | | oneSubErrorMsg = c.ErrMsg; |
| | 0 | 2590 | | } |
| | 0 | 2591 | | if (t.Equals(sub)) { |
| | 0 | 2592 | | uppers.Add(super); |
| | 0 | 2593 | | oneSuperErrorMsg = c.ErrMsg; |
| | 0 | 2594 | | } |
| | 0 | 2595 | | } |
| | | 2596 | | |
| | 0 | 2597 | | bool done = false; |
| | 0 | 2598 | | foreach (var tl in lowers) { |
| | 0 | 2599 | | foreach (var tu in uppers) { |
| | 0 | 2600 | | if (tl.Equals(tu)) { |
| | 0 | 2601 | | if (!ContainsAsTypeParameter(tu, t)) { |
| | 0 | 2602 | | var errorMsg = new TypeConstraint.ErrorMsgWithBase(AllTypeConstraints[0].ErrMsg, |
| | 0 | 2603 | | "Decision: {0} is decided to be {1} because the latter is both the upper and lower bound to th |
| | 0 | 2604 | | t, tu); |
| | 0 | 2605 | | ConstrainSubtypeRelation_Equal(t, tu, errorMsg); |
| | | 2606 | | // The above changes t so that it is a proxy with an assigned type |
| | 0 | 2607 | | anyNewConstraints = true; |
| | 0 | 2608 | | done = true; |
| | 0 | 2609 | | break; |
| | | 2610 | | } |
| | 0 | 2611 | | } |
| | 0 | 2612 | | } |
| | 0 | 2613 | | if (done) { |
| | 0 | 2614 | | break; |
| | | 2615 | | } |
| | 0 | 2616 | | } |
| | 0 | 2617 | | } |
| | 0 | 2618 | | if (anyNewConstraints) { |
| | 0 | 2619 | | break; |
| | | 2620 | | } |
| | | 2621 | | |
| | 0 | 2622 | | foreach (var t in ss) { |
| | 0 | 2623 | | var lowers = new HashSet<Type>(); |
| | 0 | 2624 | | var uppers = new HashSet<Type>(); |
| | 0 | 2625 | | foreach (var c in AllTypeConstraints) { |
| | 0 | 2626 | | var super = c.Super.NormalizeExpand(); |
| | 0 | 2627 | | var sub = c.Sub.NormalizeExpand(); |
| | 0 | 2628 | | if (t.Equals(super)) { |
| | 0 | 2629 | | lowers.Add(sub); |
| | 0 | 2630 | | } |
| | | 2631 | | |
| | 0 | 2632 | | if (t.Equals(sub)) { |
| | 0 | 2633 | | uppers.Add(super); |
| | 0 | 2634 | | } |
| | 0 | 2635 | | } |
| | | 2636 | | |
| | 0 | 2637 | | if (uppers.Count == 0) { |
| | 0 | 2638 | | if (lowers.Count == 1) { |
| | 0 | 2639 | | var em = lowers.GetEnumerator(); |
| | 0 | 2640 | | em.MoveNext(); |
| | 0 | 2641 | | if (!ContainsAsTypeParameter(em.Current, t)) { |
| | 0 | 2642 | | var errorMsg = new TypeConstraint.ErrorMsgWithBase(oneSubErrorMsg, |
| | 0 | 2643 | | "Decision: {0} is decided to be {1} because the latter is a lower bound to the proxy and there i |
| | 0 | 2644 | | t, em.Current); |
| | 0 | 2645 | | ConstrainSubtypeRelation_Equal(t, em.Current, errorMsg); |
| | 0 | 2646 | | anyNewConstraints = true; |
| | 0 | 2647 | | break; |
| | | 2648 | | } |
| | 0 | 2649 | | } |
| | 0 | 2650 | | } |
| | 0 | 2651 | | if (lowers.Count == 0) { |
| | 0 | 2652 | | if (uppers.Count == 1) { |
| | 0 | 2653 | | var em = uppers.GetEnumerator(); |
| | 0 | 2654 | | em.MoveNext(); |
| | 0 | 2655 | | if (!ContainsAsTypeParameter(em.Current, t)) { |
| | 0 | 2656 | | var errorMsg = new TypeConstraint.ErrorMsgWithBase(oneSuperErrorMsg, |
| | 0 | 2657 | | "Decision: {0} is decided to be {1} because the latter is an upper bound to the proxy and there |
| | 0 | 2658 | | t, em.Current); |
| | 0 | 2659 | | ConstrainSubtypeRelation_Equal(t, em.Current, errorMsg); |
| | 0 | 2660 | | anyNewConstraints = true; |
| | 0 | 2661 | | break; |
| | | 2662 | | } |
| | 0 | 2663 | | } |
| | 0 | 2664 | | } |
| | 0 | 2665 | | } |
| | | 2666 | | |
| | 0 | 2667 | | break; |
| | | 2668 | | } |
| | | 2669 | | |
| | | 2670 | | case 12: |
| | | 2671 | | // we're so out of here |
| | 0 | 2672 | | return; |
| | | 2673 | | } |
| | 2299920 | 2674 | | if (anyNewConstraints) { |
| | 681230 | 2675 | | state = 0; |
| | 1618690 | 2676 | | } else { |
| | 937460 | 2677 | | state++; |
| | 937460 | 2678 | | } |
| | 1618690 | 2679 | | } |
| | 102220 | 2680 | | } |
| | | 2681 | | |
| | 16800 | 2682 | | TypeProxy NewIntegerBasedProxy(IToken tok) { |
| | | 2683 | | Contract.Requires(tok != null); |
| | 16800 | 2684 | | var proxy = new InferredTypeProxy(); |
| | 16800 | 2685 | | ConstrainSubtypeRelation(new IntVarietiesSupertype(), proxy, tok, "integer literal used as if it had type {0}", pr |
| | 16800 | 2686 | | return proxy; |
| | 16800 | 2687 | | } |
| | | 2688 | | |
| | 0 | 2689 | | private bool ContainsAsTypeParameter(Type t, Type u) { |
| | 0 | 2690 | | if (t.Equals(u)) { |
| | 0 | 2691 | | return true; |
| | | 2692 | | } |
| | | 2693 | | |
| | 0 | 2694 | | if (t is UserDefinedType udt) { |
| | 0 | 2695 | | foreach (var tp in udt.TypeArgs) { |
| | 0 | 2696 | | if (ContainsAsTypeParameter(tp, u)) { |
| | 0 | 2697 | | return true; |
| | | 2698 | | } |
| | 0 | 2699 | | } |
| | 0 | 2700 | | } |
| | 0 | 2701 | | if (t is CollectionType st) { |
| | 0 | 2702 | | foreach (var tp in st.TypeArgs) { |
| | 0 | 2703 | | if (ContainsAsTypeParameter(tp, u)) { |
| | 0 | 2704 | | return true; |
| | | 2705 | | } |
| | 0 | 2706 | | } |
| | 0 | 2707 | | } |
| | 0 | 2708 | | return false; |
| | 0 | 2709 | | } |
| | | 2710 | | |
| | 52211270 | 2711 | | private void AddAllProxies(Type type, HashSet<TypeProxy> proxies) { |
| | | 2712 | | Contract.Requires(type != null); |
| | | 2713 | | Contract.Requires(proxies != null); |
| | 52211270 | 2714 | | var proxy = type as TypeProxy; |
| | 78724530 | 2715 | | if (proxy != null) { |
| | 26513260 | 2716 | | proxies.Add(proxy); |
| | 52211270 | 2717 | | } else { |
| | 107218530 | 2718 | | foreach (var ta in type.TypeArgs) { |
| | 10041500 | 2719 | | AddAllProxies(ta, proxies); |
| | 10041500 | 2720 | | } |
| | 25698010 | 2721 | | } |
| | 52211270 | 2722 | | } |
| | | 2723 | | |
| | | 2724 | | /// <summary> |
| | | 2725 | | /// Set "lhs" to the join of "rhss" and "lhs.Subtypes, if possible. |
| | | 2726 | | /// Returns "true' if something was done, or "false" otherwise. |
| | | 2727 | | /// </summary> |
| | 132709290 | 2728 | | private bool ProcessAssignable(TypeProxy lhs, List<Type> rhss) { |
| | | 2729 | | Contract.Requires(lhs != null && lhs.T == null); |
| | | 2730 | | Contract.Requires(rhss != null); |
| | 132709290 | 2731 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 2732 | | Console.Write("DEBUG: ProcessAssignable: {0} with rhss:", lhs); |
| | 0 | 2733 | | foreach (var rhs in rhss) { |
| | 0 | 2734 | | Options.OutputWriter.Write(" {0}", rhs); |
| | 0 | 2735 | | } |
| | 0 | 2736 | | Options.OutputWriter.Write(" subtypes:"); |
| | 0 | 2737 | | foreach (var sub in lhs.SubtypesKeepConstraints) { |
| | 0 | 2738 | | Options.OutputWriter.Write(" {0}", sub); |
| | 0 | 2739 | | } |
| | 0 | 2740 | | Options.OutputWriter.WriteLine(); |
| | 0 | 2741 | | } |
| | 132709290 | 2742 | | Type join = null; |
| | 560790260 | 2743 | | foreach (var rhs in rhss) { |
| | 228886710 | 2744 | | if (rhs is TypeProxy) { return false; } |
| | 4316250 | 2745 | | join = join == null ? rhs : Type.Join(join, rhs, builtIns); |
| | 4316250 | 2746 | | } |
| | 288572040 | 2747 | | foreach (var sub in lhs.SubtypesKeepConstraints) { |
| | 172468220 | 2748 | | if (sub is TypeProxy) { return false; } |
| | 15350 | 2749 | | join = join == null ? sub : Type.Join(join, sub, builtIns); |
| | 15350 | 2750 | | } |
| | 466770 | 2751 | | if (join == null) { |
| | 98590 | 2752 | | return false; |
| | 269590 | 2753 | | } else if (Reaches(join, lhs, 1, new HashSet<TypeProxy>())) { |
| | | 2754 | | // would cause a cycle, so don't do it |
| | 0 | 2755 | | return false; |
| | 269590 | 2756 | | } else { |
| | 269590 | 2757 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 2758 | | Options.OutputWriter.WriteLine("DEBUG: ProcessAssignable: assigning proxy {0}.T := {1}", lhs, join); |
| | 0 | 2759 | | } |
| | 269590 | 2760 | | lhs.T = join; |
| | 269590 | 2761 | | return true; |
| | | 2762 | | } |
| | 132709290 | 2763 | | } |
| | | 2764 | | |
| | | 2765 | | /// <summary> |
| | | 2766 | | /// Convert each Assignable(A, B) constraint into a subtyping constraint A :> B, |
| | | 2767 | | /// provided that: |
| | | 2768 | | /// - B is a non-proxy, and |
| | | 2769 | | /// - either "proxySpecialization" is null or some proxy in "proxySpecializations" prominently appears in A. |
| | | 2770 | | /// </summary> |
| | 41210 | 2771 | | bool ConvertAssignableToSubtypeConstraints(ISet<TypeProxy>/*?*/ proxySpecializations) { |
| | 41210 | 2772 | | var anyNewConstraints = false; |
| | | 2773 | | // If (the head of) the RHS of an Assignable is known, convert the XConstraint into a subtyping constraint |
| | 41210 | 2774 | | var allX = AllXConstraints; |
| | 41210 | 2775 | | AllXConstraints = new List<XConstraint>(); |
| | 56720460 | 2776 | | foreach (var xc in allX) { |
| | 27547970 | 2777 | | if (xc.ConstraintName == "Assignable" && xc.Types[1].Normalize() is NonProxyType) { |
| | 8682360 | 2778 | | var t0 = xc.Types[0].NormalizeExpand(); |
| | 8682360 | 2779 | | if (proxySpecializations == null |
| | 8682360 | 2780 | | || proxySpecializations.Contains(t0) |
| | 8936070 | 2781 | | || t0.TypeArgs.Exists(ta => proxySpecializations.Contains(ta))) { |
| | 253690 | 2782 | | ConstrainSubtypeRelation(t0, xc.Types[1], xc.errorMsg, true); |
| | 253690 | 2783 | | anyNewConstraints = true; |
| | 253690 | 2784 | | continue; |
| | | 2785 | | } |
| | 8428670 | 2786 | | } |
| | 18611920 | 2787 | | AllXConstraints.Add(xc); |
| | 18611920 | 2788 | | } |
| | 41210 | 2789 | | return anyNewConstraints; |
| | 41210 | 2790 | | } |
| | | 2791 | | |
| | 20600 | 2792 | | bool TightenUpEquatable(ISet<TypeProxy> proxiesOfInterest) { |
| | | 2793 | | Contract.Requires(proxiesOfInterest != null); |
| | 20600 | 2794 | | var anyNewConstraints = false; |
| | 20600 | 2795 | | var allX = AllXConstraints; |
| | 20600 | 2796 | | AllXConstraints = new List<XConstraint>(); |
| | 54496770 | 2797 | | foreach (var xc in allX) { |
| | 18166910 | 2798 | | if (xc.ConstraintName == "Equatable" || xc.ConstraintName == "EquatableArg") { |
| | 21920 | 2799 | | var t0 = xc.Types[0].NormalizeExpandKeepConstraints(); |
| | 21920 | 2800 | | var t1 = xc.Types[1].NormalizeExpandKeepConstraints(); |
| | 21950 | 2801 | | if (proxiesOfInterest.Contains(t0) || proxiesOfInterest.Contains(t1)) { |
| | 30 | 2802 | | ConstrainSubtypeRelation_Equal(t0, t1, xc.errorMsg); |
| | 30 | 2803 | | anyNewConstraints = true; |
| | 30 | 2804 | | continue; |
| | | 2805 | | } |
| | 21890 | 2806 | | } |
| | 18144960 | 2807 | | AllXConstraints.Add(xc); |
| | 18144960 | 2808 | | } |
| | 20600 | 2809 | | return anyNewConstraints; |
| | 20600 | 2810 | | } |
| | | 2811 | | |
| | 552043240 | 2812 | | void ProcessOneSubtypingConstraintAndItsSubs(TypeConstraint c, ISet<TypeConstraint> processed, bool fullStrength, re |
| | | 2813 | | Contract.Requires(c != null); |
| | | 2814 | | Contract.Requires(processed != null); |
| | 718045380 | 2815 | | if (processed.Contains(c)) { |
| | 166002140 | 2816 | | return; // our job has already been done, or is at least in progress |
| | | 2817 | | } |
| | 386041100 | 2818 | | processed.Add(c); |
| | | 2819 | | |
| | 386041100 | 2820 | | var super = c.Super.NormalizeExpandKeepConstraints(); |
| | 386041100 | 2821 | | var sub = c.Sub.NormalizeExpandKeepConstraints(); |
| | | 2822 | | // Process all subtype types before going on |
| | 386041100 | 2823 | | var subProxy = sub as TypeProxy; |
| | 768619410 | 2824 | | if (subProxy != null) { |
| | 1646045070 | 2825 | | foreach (var cc in subProxy.SubtypeConstraints) { |
| | 166103380 | 2826 | | ProcessOneSubtypingConstraintAndItsSubs(cc, processed, fullStrength, ref anyNewConstraints); |
| | 166103380 | 2827 | | } |
| | 382578310 | 2828 | | } |
| | | 2829 | | // the processing may have assigned some proxies, so we'll refresh super and sub |
| | 386041100 | 2830 | | super = super.NormalizeExpandKeepConstraints(); |
| | 386041100 | 2831 | | sub = sub.NormalizeExpandKeepConstraints(); |
| | | 2832 | | |
| | 388328950 | 2833 | | if (super.Equals(sub)) { |
| | | 2834 | | // the constraint is satisfied, so just drop it |
| | 386469330 | 2835 | | } else if ((super is NonProxyType || super is ArtificialType) && sub is NonProxyType) { |
| | 428230 | 2836 | | ImposeSubtypingConstraint(super, sub, c.ErrMsg); |
| | 428230 | 2837 | | anyNewConstraints = true; |
| | 383762400 | 2838 | | } else if (AssignKnownEnd(sub as TypeProxy, true, fullStrength)) { |
| | 9150 | 2839 | | anyNewConstraints = true; |
| | 383325020 | 2840 | | } else if (sub is TypeProxy && fullStrength && AssignKnownEndsFullstrength((TypeProxy)sub)) { |
| | 0 | 2841 | | anyNewConstraints = true; |
| | 383315870 | 2842 | | } else { |
| | | 2843 | | // keep the constraint for now |
| | 383315870 | 2844 | | AllTypeConstraints.Add(c); |
| | 383315870 | 2845 | | } |
| | 552043240 | 2846 | | } |
| | | 2847 | | |
| | 1204220 | 2848 | | void ProcessFullStrength_SubDirection(Type t, ISet<TypeProxy> processed, ref bool anyNewConstraints) { |
| | | 2849 | | Contract.Requires(t != null); |
| | | 2850 | | Contract.Requires(processed != null); |
| | 1204220 | 2851 | | var proxy = t.NormalizeExpand() as TypeProxy; |
| | 2156830 | 2852 | | if (proxy != null) { |
| | 1613320 | 2853 | | if (processed.Contains(proxy)) { |
| | 660710 | 2854 | | return; // our job has already been done, or is at least in progress |
| | | 2855 | | } |
| | 291900 | 2856 | | processed.Add(proxy); |
| | | 2857 | | |
| | 2305680 | 2858 | | foreach (var u in proxy.SubtypesKeepConstraints_WithAssignable(AllXConstraints)) { |
| | 476660 | 2859 | | ProcessFullStrength_SubDirection(u, processed, ref anyNewConstraints); |
| | 476660 | 2860 | | } |
| | 291900 | 2861 | | proxy = proxy.NormalizeExpand() as TypeProxy; |
| | 292110 | 2862 | | if (proxy != null && AssignKnownEndsFullstrength_SubDirection(proxy)) { |
| | 210 | 2863 | | anyNewConstraints = true; |
| | 210 | 2864 | | } |
| | 291900 | 2865 | | } |
| | 1204220 | 2866 | | } |
| | | 2867 | | |
| | 1140470 | 2868 | | void ProcessFullStrength_SuperDirection(Type t, ISet<TypeProxy> processed, ref bool anyNewConstraints) { |
| | | 2869 | | Contract.Requires(t != null); |
| | | 2870 | | Contract.Requires(processed != null); |
| | 1140470 | 2871 | | var proxy = t.NormalizeExpand() as TypeProxy; |
| | 1904470 | 2872 | | if (proxy != null) { |
| | 1219110 | 2873 | | if (processed.Contains(proxy)) { |
| | 455110 | 2874 | | return; // our job has already been done, or is at least in progress |
| | | 2875 | | } |
| | 308890 | 2876 | | processed.Add(proxy); |
| | | 2877 | | |
| | 2341800 | 2878 | | foreach (var u in proxy.Supertypes) { |
| | 471710 | 2879 | | ProcessFullStrength_SuperDirection(u, processed, ref anyNewConstraints); |
| | 471710 | 2880 | | } |
| | 308890 | 2881 | | proxy = proxy.NormalizeExpand() as TypeProxy; |
| | 396900 | 2882 | | if (proxy != null && AssignKnownEndsFullstrength_SuperDirection(proxy)) { |
| | 88010 | 2883 | | anyNewConstraints = true; |
| | 88010 | 2884 | | } |
| | 308890 | 2885 | | } |
| | 1140470 | 2886 | | } |
| | | 2887 | | |
| | | 2888 | | /// <summary> |
| | | 2889 | | /// Returns true if anything happened. |
| | | 2890 | | /// </summary> |
| | 768135740 | 2891 | | bool AssignKnownEnd(TypeProxy proxy, bool keepConstraints, bool fullStrength) { |
| | | 2892 | | Contract.Requires(proxy == null || proxy.T == null); // caller is supposed to have called NormalizeExpand |
| | 928684430 | 2893 | | if (proxy == null) { |
| | | 2894 | | // nothing to do |
| | 160548690 | 2895 | | return false; |
| | | 2896 | | } |
| | | 2897 | | // ----- first, go light; also, prefer subtypes over supertypes |
| | 607587050 | 2898 | | IEnumerable<Type> subTypes = keepConstraints ? proxy.SubtypesKeepConstraints : proxy.Subtypes; |
| | 501692074 | 2899 | | foreach (var su in subTypes) { |
| | 991452820 | 2900 | | DetermineRootLeaf(su, out var isRoot, out _, out var headRoot, out _); |
| | 991452820 | 2901 | | Contract.Assert(!isRoot || headRoot); // isRoot ==> headRoot |
| | 991798660 | 2902 | | if (isRoot) { |
| | 345840 | 2903 | | if (Reaches(su, proxy, 1, new HashSet<TypeProxy>())) { |
| | | 2904 | | // adding a constraint here would cause a bad cycle, so we don't |
| | 345840 | 2905 | | } else { |
| | 345840 | 2906 | | AssignProxyAndHandleItsConstraints(proxy, su, keepConstraints); |
| | 345840 | 2907 | | return true; |
| | | 2908 | | } |
| | 991221380 | 2909 | | } else if (headRoot) { |
| | 114400 | 2910 | | if (Reaches(su, proxy, 1, new HashSet<TypeProxy>())) { |
| | | 2911 | | // adding a constraint here would cause a bad cycle, so we don't |
| | 114400 | 2912 | | } else { |
| | 114400 | 2913 | | AssignProxyAndHandleItsConstraints(proxy, TypeProxy.HeadWithProxyArgs(su), keepConstraints); |
| | 114400 | 2914 | | return true; |
| | | 2915 | | } |
| | 0 | 2916 | | } |
| | 990992580 | 2917 | | } |
| | 607126810 | 2918 | | if (fullStrength) { |
| | 0 | 2919 | | IEnumerable<Type> superTypes = keepConstraints ? proxy.SupertypesKeepConstraints : proxy.Supertypes; |
| | 0 | 2920 | | foreach (var su in superTypes) { |
| | 0 | 2921 | | DetermineRootLeaf(su, out _, out var isLeaf, out _, out var headLeaf); |
| | 0 | 2922 | | Contract.Assert(!isLeaf || headLeaf); // isLeaf ==> headLeaf |
| | 0 | 2923 | | if (isLeaf) { |
| | 0 | 2924 | | if (Reaches(su, proxy, -1, new HashSet<TypeProxy>())) { |
| | | 2925 | | // adding a constraint here would cause a bad cycle, so we don't |
| | 0 | 2926 | | } else { |
| | 0 | 2927 | | AssignProxyAndHandleItsConstraints(proxy, su, keepConstraints); |
| | 0 | 2928 | | return true; |
| | | 2929 | | } |
| | 0 | 2930 | | } else if (headLeaf) { |
| | 0 | 2931 | | if (Reaches(su, proxy, -1, new HashSet<TypeProxy>())) { |
| | | 2932 | | // adding a constraint here would cause a bad cycle, so we don't |
| | 0 | 2933 | | } else { |
| | 0 | 2934 | | AssignProxyAndHandleItsConstraints(proxy, TypeProxy.HeadWithProxyArgs(su), keepConstraints); |
| | 0 | 2935 | | return true; |
| | | 2936 | | } |
| | 0 | 2937 | | } |
| | 0 | 2938 | | } |
| | 0 | 2939 | | } |
| | 607126810 | 2940 | | return false; |
| | 768135740 | 2941 | | } |
| | | 2942 | | |
| | 0 | 2943 | | bool AssignKnownEndsFullstrength(TypeProxy proxy) { |
| | | 2944 | | Contract.Requires(proxy != null); |
| | | 2945 | | // ----- continue with full strength |
| | | 2946 | | // If the join of the subtypes exists, use it |
| | 0 | 2947 | | var joins = new List<Type>(); |
| | 0 | 2948 | | foreach (var su in proxy.Subtypes) { |
| | 0 | 2949 | | if (su is TypeProxy) { |
| | 0 | 2950 | | continue; // don't include proxies in the meet computation |
| | | 2951 | | } |
| | 0 | 2952 | | int i = 0; |
| | 0 | 2953 | | for (; i < joins.Count; i++) { |
| | 0 | 2954 | | var j = Type.Join(joins[i], su, builtIns); |
| | 0 | 2955 | | if (j != null) { |
| | 0 | 2956 | | joins[i] = j; |
| | 0 | 2957 | | break; |
| | | 2958 | | } |
| | 0 | 2959 | | } |
| | 0 | 2960 | | if (i == joins.Count) { |
| | | 2961 | | // we went to the end without finding a place to meet up |
| | 0 | 2962 | | joins.Add(su); |
| | 0 | 2963 | | } |
| | 0 | 2964 | | } |
| | 0 | 2965 | | if (joins.Count == 1 && !Reaches(joins[0], proxy, 1, new HashSet<TypeProxy>())) { |
| | | 2966 | | // we were able to compute a meet of all the subtyping constraints, so use it |
| | 0 | 2967 | | AssignProxyAndHandleItsConstraints(proxy, joins[0]); |
| | 0 | 2968 | | return true; |
| | | 2969 | | } |
| | | 2970 | | // If the meet of the supertypes exists, use it |
| | 0 | 2971 | | var meets = new List<Type>(); |
| | 0 | 2972 | | foreach (var su in proxy.Supertypes) { |
| | 0 | 2973 | | if (su is TypeProxy) { |
| | 0 | 2974 | | continue; // don't include proxies in the meet computation |
| | | 2975 | | } |
| | 0 | 2976 | | int i = 0; |
| | 0 | 2977 | | for (; i < meets.Count; i++) { |
| | 0 | 2978 | | var j = Type.Meet(meets[i], su, builtIns); |
| | 0 | 2979 | | if (j != null) { |
| | 0 | 2980 | | meets[i] = j; |
| | 0 | 2981 | | break; |
| | | 2982 | | } |
| | 0 | 2983 | | } |
| | 0 | 2984 | | if (i == meets.Count) { |
| | | 2985 | | // we went to the end without finding a place to meet |
| | 0 | 2986 | | meets.Add(su); |
| | 0 | 2987 | | } |
| | 0 | 2988 | | } |
| | 0 | 2989 | | if (meets.Count == 1 && !(meets[0] is ArtificialType) && !Reaches(meets[0], proxy, -1, new HashSet<TypeProxy>())) |
| | | 2990 | | // we were able to compute a meet of all the subtyping constraints, so use it |
| | 0 | 2991 | | AssignProxyAndHandleItsConstraints(proxy, meets[0]); |
| | 0 | 2992 | | return true; |
| | | 2993 | | } |
| | | 2994 | | |
| | 0 | 2995 | | return false; |
| | 0 | 2996 | | } |
| | | 2997 | | |
| | 291880 | 2998 | | bool AssignKnownEndsFullstrength_SubDirection(TypeProxy proxy) { |
| | | 2999 | | Contract.Requires(proxy != null && proxy.T == null); |
| | | 3000 | | // If the join the subtypes exists, use it |
| | 291880 | 3001 | | var joins = new List<Type>(); |
| | 291880 | 3002 | | var proxySubs = new HashSet<TypeProxy>(); |
| | 291880 | 3003 | | proxySubs.Add(proxy); |
| | 2305500 | 3004 | | foreach (var su in proxy.SubtypesKeepConstraints_WithAssignable(AllXConstraints)) { |
| | 953010 | 3005 | | if (su is TypeProxy) { |
| | 476390 | 3006 | | proxySubs.Add((TypeProxy)su); |
| | 476620 | 3007 | | } else { |
| | 230 | 3008 | | int i = 0; |
| | 250 | 3009 | | for (; i < joins.Count; i++) { |
| | 20 | 3010 | | var j = Type.Join(joins[i], su, builtIns); |
| | 40 | 3011 | | if (j != null) { |
| | 20 | 3012 | | joins[i] = j; |
| | 20 | 3013 | | break; |
| | | 3014 | | } |
| | 0 | 3015 | | } |
| | 440 | 3016 | | if (i == joins.Count) { |
| | | 3017 | | // we went to the end without finding a place to join in |
| | 210 | 3018 | | joins.Add(su); |
| | 210 | 3019 | | } |
| | 230 | 3020 | | } |
| | 476620 | 3021 | | } |
| | 292090 | 3022 | | if (joins.Count == 1 && !Reaches(joins[0], proxy, 1, new HashSet<TypeProxy>())) { |
| | | 3023 | | // We were able to compute a join of all the subtyping constraints, so use it. |
| | | 3024 | | // Well, maybe. If "join[0]" denotes a non-null type and "proxy" is something |
| | | 3025 | | // that could be assigned "null", then set "proxy" to the nullable version of "join[0]". |
| | | 3026 | | // Stated differently, think of an applicable "IsNullableRefType" constraint as |
| | | 3027 | | // being part of the join computation, essentially throwing in a "...?". |
| | | 3028 | | // Except: If the join is a tight bound--meaning, it is also a meet--then pick it |
| | | 3029 | | // after all, because that seems to give rise to less confusing error messages. |
| | 210 | 3030 | | if (joins[0].IsNonNullRefType) { |
| | 0 | 3031 | | Type meet = null; |
| | 0 | 3032 | | if (MeetOfAllSupertypes(proxy, ref meet, new HashSet<TypeProxy>(), false) && meet != null && Type.SameHead(joi |
| | | 3033 | | // leave it |
| | 0 | 3034 | | } else { |
| | 0 | 3035 | | CloseOverAssignableRhss(proxySubs); |
| | 0 | 3036 | | if (HasApplicableNullableRefTypeConstraint(proxySubs)) { |
| | 0 | 3037 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 3038 | | Options.OutputWriter.WriteLine("DEBUG: Found join {0} for proxy {1}, but weakening it to {2}", joins[0], |
| | 0 | 3039 | | } |
| | 0 | 3040 | | AssignProxyAndHandleItsConstraints(proxy, joins[0].NormalizeExpand(), true); |
| | 0 | 3041 | | return true; |
| | | 3042 | | } |
| | 0 | 3043 | | } |
| | 0 | 3044 | | } |
| | 210 | 3045 | | AssignProxyAndHandleItsConstraints(proxy, joins[0], true); |
| | 210 | 3046 | | return true; |
| | | 3047 | | } |
| | 291670 | 3048 | | return false; |
| | 291880 | 3049 | | } |
| | | 3050 | | |
| | 0 | 3051 | | private void CloseOverAssignableRhss(ISet<TypeProxy> proxySet) { |
| | | 3052 | | Contract.Requires(proxySet != null); |
| | 0 | 3053 | | while (true) { |
| | 0 | 3054 | | var moreChanges = false; |
| | 0 | 3055 | | foreach (var xc in AllXConstraints) { |
| | 0 | 3056 | | if (xc.ConstraintName == "Assignable") { |
| | 0 | 3057 | | var source = xc.Types[0].Normalize() as TypeProxy; |
| | 0 | 3058 | | var sink = xc.Types[1].Normalize() as TypeProxy; |
| | 0 | 3059 | | if (source != null && sink != null && proxySet.Contains(source) && !proxySet.Contains(sink)) { |
| | 0 | 3060 | | proxySet.Add(sink); |
| | 0 | 3061 | | moreChanges = true; |
| | 0 | 3062 | | } |
| | 0 | 3063 | | } |
| | 0 | 3064 | | } |
| | 0 | 3065 | | if (!moreChanges) { |
| | 0 | 3066 | | return; |
| | | 3067 | | } |
| | 0 | 3068 | | } |
| | 0 | 3069 | | } |
| | 0 | 3070 | | private bool HasApplicableNullableRefTypeConstraint(ISet<TypeProxy> proxySet) { |
| | | 3071 | | Contract.Requires(proxySet != null); |
| | 0 | 3072 | | var nullableProxies = new HashSet<TypeProxy>(); |
| | 0 | 3073 | | foreach (var xc in AllXConstraints) { |
| | 0 | 3074 | | if (xc.ConstraintName == "IsNullableRefType") { |
| | 0 | 3075 | | var npr = xc.Types[0].Normalize() as TypeProxy; |
| | 0 | 3076 | | if (npr != null) { |
| | 0 | 3077 | | nullableProxies.Add(npr); |
| | 0 | 3078 | | } |
| | 0 | 3079 | | } |
| | 0 | 3080 | | } |
| | 0 | 3081 | | return proxySet.Any(nullableProxies.Contains); |
| | 0 | 3082 | | } |
| | 840 | 3083 | | private bool HasApplicableNullableRefTypeConstraint_SubDirection(TypeProxy proxy) { |
| | | 3084 | | Contract.Requires(proxy != null); |
| | 840 | 3085 | | var nullableProxies = new HashSet<TypeProxy>(); |
| | 5040 | 3086 | | foreach (var xc in AllXConstraints) { |
| | 1680 | 3087 | | if (xc.ConstraintName == "IsNullableRefType") { |
| | 840 | 3088 | | var npr = xc.Types[0].Normalize() as TypeProxy; |
| | 1680 | 3089 | | if (npr != null) { |
| | 840 | 3090 | | nullableProxies.Add(npr); |
| | 840 | 3091 | | } |
| | 840 | 3092 | | } |
| | 840 | 3093 | | } |
| | 840 | 3094 | | return HasApplicableNullableRefTypeConstraint_SubDirection_aux(proxy, nullableProxies, new HashSet<TypeProxy>()); |
| | 840 | 3095 | | } |
| | 840 | 3096 | | private bool HasApplicableNullableRefTypeConstraint_SubDirection_aux(TypeProxy proxy, ISet<TypeProxy> nullableProxie |
| | | 3097 | | Contract.Requires(proxy != null); |
| | | 3098 | | Contract.Requires(nullableProxies != null); |
| | | 3099 | | Contract.Requires(visitedProxies != null); |
| | | 3100 | | |
| | 840 | 3101 | | if (visitedProxies.Contains(proxy)) { |
| | 0 | 3102 | | return false; |
| | | 3103 | | } |
| | 840 | 3104 | | visitedProxies.Add(proxy); |
| | | 3105 | | |
| | 1680 | 3106 | | if (nullableProxies.Contains(proxy)) { |
| | 840 | 3107 | | return true; |
| | | 3108 | | } |
| | | 3109 | | |
| | 0 | 3110 | | foreach (var sub in proxy.SubtypesKeepConstraints_WithAssignable(AllXConstraints)) { |
| | 0 | 3111 | | var psub = sub as TypeProxy; |
| | 0 | 3112 | | if (psub != null && HasApplicableNullableRefTypeConstraint_SubDirection_aux(psub, nullableProxies, visitedProxie |
| | 0 | 3113 | | return true; |
| | | 3114 | | } |
| | 0 | 3115 | | } |
| | 0 | 3116 | | return false; |
| | 840 | 3117 | | } |
| | | 3118 | | |
| | 296420 | 3119 | | bool AssignKnownEndsFullstrength_SuperDirection(TypeProxy proxy) { |
| | | 3120 | | Contract.Requires(proxy != null && proxy.T == null); |
| | | 3121 | | // First, compute the the join of the Assignable LHSs. Then, compute |
| | | 3122 | | // the meet of that join and the supertypes. |
| | 296420 | 3123 | | var joins = new List<Type>(); |
| | 90381870 | 3124 | | foreach (var xc in AllXConstraints) { |
| | 29981080 | 3125 | | if (xc.ConstraintName == "Assignable" && xc.Types[1].Normalize() == proxy) { |
| | 150210 | 3126 | | var su = xc.Types[0].Normalize(); |
| | 253010 | 3127 | | if (su is TypeProxy) { |
| | 102800 | 3128 | | continue; // don't include proxies in the join computation |
| | | 3129 | | } |
| | 47410 | 3130 | | int i = 0; |
| | 48670 | 3131 | | for (; i < joins.Count; i++) { |
| | 1260 | 3132 | | var j = Type.Join(joins[i], su, builtIns); |
| | 2520 | 3133 | | if (j != null) { |
| | 1260 | 3134 | | joins[i] = j; |
| | 1260 | 3135 | | break; |
| | | 3136 | | } |
| | 0 | 3137 | | } |
| | 93560 | 3138 | | if (i == joins.Count) { |
| | | 3139 | | // we went to the end without finding a place to join in |
| | 46150 | 3140 | | joins.Add(su); |
| | 46150 | 3141 | | } |
| | 47410 | 3142 | | } |
| | 29728070 | 3143 | | } |
| | | 3144 | | // If the meet of the supertypes exists, use it |
| | 296420 | 3145 | | var meets = new List<Type>(joins); |
| | 2187510 | 3146 | | foreach (var su in proxy.SupertypesKeepConstraints) { |
| | 678620 | 3147 | | if (su is TypeProxy) { |
| | 245870 | 3148 | | continue; // don't include proxies in the meet computation |
| | | 3149 | | } |
| | 186880 | 3150 | | int i = 0; |
| | 237660 | 3151 | | for (; i < meets.Count; i++) { |
| | 50780 | 3152 | | var j = Type.Meet(meets[i], su, builtIns); |
| | 101560 | 3153 | | if (j != null) { |
| | 50780 | 3154 | | meets[i] = j; |
| | 50780 | 3155 | | break; |
| | | 3156 | | } |
| | 0 | 3157 | | } |
| | 322980 | 3158 | | if (i == meets.Count) { |
| | | 3159 | | // we went to the end without finding a place to meet up |
| | 136100 | 3160 | | meets.Add(su); |
| | 136100 | 3161 | | } |
| | 186880 | 3162 | | } |
| | 384430 | 3163 | | if (meets.Count == 1 && !(meets[0] is ArtificialType) && !Reaches(meets[0], proxy, -1, new HashSet<TypeProxy>())) |
| | | 3164 | | // we were able to compute a meet of all the subtyping constraints, so use it |
| | 88010 | 3165 | | AssignProxyAndHandleItsConstraints(proxy, meets[0], true); |
| | 88010 | 3166 | | return true; |
| | | 3167 | | } |
| | 208410 | 3168 | | return false; |
| | 296420 | 3169 | | } |
| | | 3170 | | |
| | | 3171 | | int _reaches_recursion; |
| | 2700190 | 3172 | | private bool Reaches(Type t, TypeProxy proxy, int direction, HashSet<TypeProxy> visited) { |
| | 2700190 | 3173 | | if (_reaches_recursion == 20) { |
| | 0 | 3174 | | Contract.Assume(false); // possible infinite recursion |
| | 0 | 3175 | | } |
| | 2700190 | 3176 | | _reaches_recursion++; |
| | 2700190 | 3177 | | var b = Reaches_aux(t, proxy, direction, visited); |
| | 2700190 | 3178 | | _reaches_recursion--; |
| | 2700190 | 3179 | | return b; |
| | 2700190 | 3180 | | } |
| | 2700190 | 3181 | | private bool Reaches_aux(Type t, TypeProxy proxy, int direction, HashSet<TypeProxy> visited) { |
| | | 3182 | | Contract.Requires(t != null); |
| | | 3183 | | Contract.Requires(proxy != null); |
| | | 3184 | | Contract.Requires(visited != null); |
| | 2700190 | 3185 | | t = t.NormalizeExpand(); |
| | 2700190 | 3186 | | var tproxy = t as TypeProxy; |
| | 4381670 | 3187 | | if (tproxy == null) { |
| | 1681480 | 3188 | | var polarities = Type.GetPolarities(t).ConvertAll(TypeParameter.Direction); |
| | 1681480 | 3189 | | Contract.Assert(polarities != null); |
| | 1681480 | 3190 | | Contract.Assert(polarities.Count <= t.TypeArgs.Count); |
| | 7029770 | 3191 | | for (int i = 0; i < polarities.Count; i++) { |
| | 1222270 | 3192 | | if (Reaches(t.TypeArgs[i], proxy, direction * polarities[i], visited)) { |
| | 0 | 3193 | | return true; |
| | | 3194 | | } |
| | 1222270 | 3195 | | } |
| | 1681480 | 3196 | | return false; |
| | 1018810 | 3197 | | } else if (tproxy == proxy) { |
| | 100 | 3198 | | return true; |
| | 1306480 | 3199 | | } else if (visited.Contains(tproxy)) { |
| | 287870 | 3200 | | return false; |
| | 730740 | 3201 | | } else { |
| | 730740 | 3202 | | visited.Add(tproxy); |
| | 1390140 | 3203 | | if (0 <= direction && tproxy.Subtypes.Any(su => Reaches(su, proxy, direction, visited))) { |
| | 0 | 3204 | | return true; |
| | | 3205 | | } |
| | 730740 | 3206 | | if (direction <= 0 && tproxy.Supertypes.Any(su => Reaches(su, proxy, direction, visited))) { |
| | 0 | 3207 | | return true; |
| | | 3208 | | } |
| | 730740 | 3209 | | return false; |
| | | 3210 | | } |
| | 2700190 | 3211 | | } |
| | | 3212 | | |
| | | 3213 | | /// <summary> |
| | | 3214 | | /// Assumes type parameters have already been pushed, and that all types in class members have been resolved |
| | | 3215 | | /// </summary> |
| | 1680 | 3216 | | void ResolveClassMemberBodiesInitial(TopLevelDeclWithMembers cl) { |
| | | 3217 | | Contract.Requires(cl != null); |
| | | 3218 | | Contract.Requires(currentClass == null); |
| | | 3219 | | Contract.Requires(AllTypeConstraints.Count == 0); |
| | | 3220 | | Contract.Ensures(currentClass == null); |
| | | 3221 | | Contract.Ensures(AllTypeConstraints.Count == 0); |
| | | 3222 | | |
| | 1680 | 3223 | | currentClass = cl; |
| | 56370 | 3224 | | foreach (MemberDecl member in cl.Members) { |
| | 17110 | 3225 | | Contract.Assert(VisibleInScope(member)); |
| | 17110 | 3226 | | if (member is ConstantField { Rhs: { } } constantField) { |
| | 0 | 3227 | | var resolutionContext = new ResolutionContext(constantField, false); |
| | 0 | 3228 | | scope.PushMarker(); |
| | 0 | 3229 | | if (constantField.IsStatic || currentClass == null || !currentClass.AcceptThis) { |
| | 0 | 3230 | | scope.AllowInstance = false; |
| | 0 | 3231 | | } |
| | 0 | 3232 | | ResolveExpression(constantField.Rhs, resolutionContext); |
| | 0 | 3233 | | scope.PopMarker(); |
| | 0 | 3234 | | AddAssignableConstraint(constantField.tok, constantField.Type, constantField.Rhs.Type, |
| | 0 | 3235 | | "type for constant '" + constantField.Name + "' is '{0}', but its initialization value type is '{1}'"); |
| | 0 | 3236 | | SolveAllTypeConstraints(); |
| | 0 | 3237 | | } |
| | 17110 | 3238 | | } |
| | 1680 | 3239 | | currentClass = null; |
| | 1680 | 3240 | | } |
| | | 3241 | | |
| | | 3242 | | /// <summary> |
| | | 3243 | | /// Assumes type parameters have already been pushed, and that all types in class members have been resolved |
| | | 3244 | | /// </summary> |
| | 1680 | 3245 | | void ResolveClassMemberBodies(TopLevelDeclWithMembers cl) { |
| | | 3246 | | Contract.Requires(cl != null); |
| | | 3247 | | Contract.Requires(currentClass == null); |
| | | 3248 | | Contract.Requires(AllTypeConstraints.Count == 0); |
| | | 3249 | | Contract.Ensures(currentClass == null); |
| | | 3250 | | Contract.Ensures(AllTypeConstraints.Count == 0); |
| | | 3251 | | |
| | 1680 | 3252 | | currentClass = cl; |
| | 56370 | 3253 | | foreach (MemberDecl member in cl.Members) { |
| | 17110 | 3254 | | Contract.Assert(VisibleInScope(member)); |
| | 17530 | 3255 | | if (member is Field) { |
| | 420 | 3256 | | var resolutionContext = new ResolutionContext(new NoContext(currentClass.EnclosingModuleDefinition), false); |
| | 420 | 3257 | | scope.PushMarker(); |
| | 420 | 3258 | | if (member.IsStatic) { |
| | 0 | 3259 | | scope.AllowInstance = false; |
| | 0 | 3260 | | } |
| | 420 | 3261 | | ResolveAttributes(member, resolutionContext, true); |
| | 420 | 3262 | | scope.PopMarker(); |
| | | 3263 | | |
| | 29280 | 3264 | | } else if (member is Function function) { |
| | 12170 | 3265 | | var ec = reporter.Count(ErrorLevel.Error); |
| | 12170 | 3266 | | allTypeParameters.PushMarker(); |
| | 12170 | 3267 | | ResolveTypeParameters(function.TypeArgs, false, function); |
| | | 3268 | | |
| | 12170 | 3269 | | function.Resolve(this); |
| | 12170 | 3270 | | allTypeParameters.PopMarker(); |
| | 12170 | 3271 | | if (function is ExtremePredicate { PrefixPredicate: { } prefixPredicate } && ec == reporter.Count(ErrorLevel.E |
| | 0 | 3272 | | allTypeParameters.PushMarker(); |
| | 0 | 3273 | | ResolveTypeParameters(prefixPredicate.TypeArgs, false, prefixPredicate); |
| | 0 | 3274 | | prefixPredicate.Resolve(this); |
| | 0 | 3275 | | allTypeParameters.PopMarker(); |
| | 0 | 3276 | | } |
| | | 3277 | | |
| | 21210 | 3278 | | } else if (member is Method method) { |
| | 4520 | 3279 | | var ec = reporter.Count(ErrorLevel.Error); |
| | 4520 | 3280 | | allTypeParameters.PushMarker(); |
| | 4520 | 3281 | | ResolveTypeParameters(method.TypeArgs, false, method); |
| | 4520 | 3282 | | method.Resolve(this); |
| | 4520 | 3283 | | allTypeParameters.PopMarker(); |
| | 4520 | 3284 | | if (method is ExtremeLemma { PrefixLemma: { } prefixLemma } && ec == reporter.Count(ErrorLevel.Error)) { |
| | 0 | 3285 | | allTypeParameters.PushMarker(); |
| | 0 | 3286 | | ResolveTypeParameters(prefixLemma.TypeArgs, false, prefixLemma); |
| | 0 | 3287 | | prefixLemma.Resolve(this); |
| | 0 | 3288 | | allTypeParameters.PopMarker(); |
| | 0 | 3289 | | } |
| | | 3290 | | |
| | 4520 | 3291 | | } else { |
| | 0 | 3292 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected member type |
| | | 3293 | | } |
| | 17110 | 3294 | | Contract.Assert(AllTypeConstraints.Count == 0); |
| | 17110 | 3295 | | } |
| | 1680 | 3296 | | currentClass = null; |
| | 1680 | 3297 | | } |
| | | 3298 | | |
| | | 3299 | | /// <summary> |
| | | 3300 | | /// Assumes type parameters have already been pushed |
| | | 3301 | | /// </summary> |
| | 0 | 3302 | | void ResolveCtorTypes(DatatypeDecl/*!*/ dt, Graph<IndDatatypeDecl/*!*/>/*!*/ dependencies, Graph<CoDatatypeDecl/*!*/ |
| | | 3303 | | Contract.Requires(dt != null); |
| | | 3304 | | Contract.Requires(dependencies != null); |
| | | 3305 | | Contract.Requires(coDependencies != null); |
| | 0 | 3306 | | foreach (DatatypeCtor ctor in dt.Ctors) { |
| | | 3307 | | |
| | 0 | 3308 | | ctor.EnclosingDatatype = dt; |
| | | 3309 | | |
| | 0 | 3310 | | allTypeParameters.PushMarker(); |
| | 0 | 3311 | | ResolveCtorSignature(ctor, dt.TypeArgs); |
| | 0 | 3312 | | allTypeParameters.PopMarker(); |
| | | 3313 | | |
| | 0 | 3314 | | if (dt is IndDatatypeDecl) { |
| | | 3315 | | // The dependencies of interest among inductive datatypes are all (inductive data)types mentioned in the param |
| | 0 | 3316 | | var idt = (IndDatatypeDecl)dt; |
| | 0 | 3317 | | dependencies.AddVertex(idt); |
| | 0 | 3318 | | foreach (Formal p in ctor.Formals) { |
| | 0 | 3319 | | AddDatatypeDependencyEdge(idt, p.Type, dependencies); |
| | 0 | 3320 | | } |
| | 0 | 3321 | | } else { |
| | | 3322 | | // The dependencies of interest among codatatypes are just the top-level types of parameters. |
| | 0 | 3323 | | var codt = (CoDatatypeDecl)dt; |
| | 0 | 3324 | | coDependencies.AddVertex(codt); |
| | 0 | 3325 | | foreach (var p in ctor.Formals) { |
| | 0 | 3326 | | var co = p.Type.AsCoDatatype; |
| | 0 | 3327 | | if (co != null && codt.EnclosingModuleDefinition == co.EnclosingModuleDefinition) { |
| | 0 | 3328 | | coDependencies.AddEdge(codt, co); |
| | 0 | 3329 | | } |
| | 0 | 3330 | | } |
| | 0 | 3331 | | } |
| | 0 | 3332 | | } |
| | 0 | 3333 | | } |
| | | 3334 | | |
| | 0 | 3335 | | void ResolveCtorSignature(DatatypeCtor ctor, List<TypeParameter> dtTypeArguments) { |
| | | 3336 | | Contract.Requires(ctor != null); |
| | | 3337 | | Contract.Requires(ctor.EnclosingDatatype != null); |
| | | 3338 | | Contract.Requires(dtTypeArguments != null); |
| | 0 | 3339 | | foreach (Formal p in ctor.Formals) { |
| | 0 | 3340 | | ResolveType(p.tok, p.Type, ctor.EnclosingDatatype, ResolveTypeOptionEnum.AllowPrefix, dtTypeArguments); |
| | 0 | 3341 | | } |
| | 0 | 3342 | | } |
| | | 3343 | | |
| | 0 | 3344 | | void AddDatatypeDependencyEdge(IndDatatypeDecl dt, Type tp, Graph<IndDatatypeDecl> dependencies) { |
| | | 3345 | | Contract.Requires(dt != null); |
| | | 3346 | | Contract.Requires(tp != null); |
| | | 3347 | | Contract.Requires(dependencies != null); // more expensive check: Contract.Requires(cce.NonNullElements(dependenc |
| | | 3348 | | |
| | 0 | 3349 | | tp = tp.NormalizeExpand(); |
| | 0 | 3350 | | var dependee = tp.AsIndDatatype; |
| | 0 | 3351 | | if (dependee != null && dt.EnclosingModuleDefinition == dependee.EnclosingModuleDefinition) { |
| | 0 | 3352 | | dependencies.AddEdge(dt, dependee); |
| | 0 | 3353 | | foreach (var ta in ((UserDefinedType)tp).TypeArgs) { |
| | 0 | 3354 | | AddDatatypeDependencyEdge(dt, ta, dependencies); |
| | 0 | 3355 | | } |
| | 0 | 3356 | | } |
| | 0 | 3357 | | } |
| | | 3358 | | |
| | 0 | 3359 | | public void ResolveFrameExpressionTopLevel(FrameExpression fe, FrameExpressionUse use, ICodeContext codeContext) { |
| | 0 | 3360 | | ResolveFrameExpression(fe, use, new ResolutionContext(codeContext, false)); |
| | 0 | 3361 | | } |
| | | 3362 | | |
| | 0 | 3363 | | void ResolveFrameExpression(FrameExpression fe, FrameExpressionUse use, ResolutionContext resolutionContext) { |
| | | 3364 | | Contract.Requires(fe != null); |
| | | 3365 | | Contract.Requires(resolutionContext != null); |
| | | 3366 | | |
| | 0 | 3367 | | ResolveExpression(fe.E, resolutionContext); |
| | 0 | 3368 | | Type t = fe.E.Type; |
| | 0 | 3369 | | Contract.Assert(t != null); // follows from postcondition of ResolveExpression |
| | 0 | 3370 | | var eventualRefType = new InferredTypeProxy(); |
| | 0 | 3371 | | if (use == FrameExpressionUse.Reads) { |
| | 0 | 3372 | | AddXConstraint(fe.E.tok, "ReadsFrame", t, eventualRefType, |
| | 0 | 3373 | | "a reads-clause expression must denote an object, a set/iset/multiset/seq of objects, or a function to a set/i |
| | 0 | 3374 | | } else { |
| | 0 | 3375 | | AddXConstraint(fe.E.tok, "ModifiesFrame", t, eventualRefType, |
| | 0 | 3376 | | use == FrameExpressionUse.Modifies ? |
| | 0 | 3377 | | "a modifies-clause expression must denote an object or a set/iset/multiset/seq of objects (instead got {0})" : |
| | 0 | 3378 | | "an unchanged expression must denote an object or a set/iset/multiset/seq of objects (instead got {0})"); |
| | 0 | 3379 | | } |
| | 0 | 3380 | | if (fe.FieldName != null) { |
| | 0 | 3381 | | var member = ResolveMember(fe.E.tok, eventualRefType, fe.FieldName, out var tentativeReceiverType); |
| | 0 | 3382 | | var ctype = (UserDefinedType)tentativeReceiverType; // correctness of cast follows from the DenotesClass test a |
| | 0 | 3383 | | if (member == null) { |
| | | 3384 | | // error has already been reported by ResolveMember |
| | 0 | 3385 | | } else if (!(member is Field)) { |
| | 0 | 3386 | | reporter.Error(MessageSource.Resolver, fe.E, "member {0} in type {1} does not refer to a field", fe.FieldName, |
| | 0 | 3387 | | } else if (member is ConstantField) { |
| | 0 | 3388 | | reporter.Error(MessageSource.Resolver, fe.E, "expression is not allowed to refer to constant field {0}", fe.Fi |
| | 0 | 3389 | | } else { |
| | 0 | 3390 | | Contract.Assert(ctype != null && ctype.ResolvedClass != null); // follows from postcondition of ResolveMember |
| | 0 | 3391 | | fe.Field = (Field)member; |
| | 0 | 3392 | | } |
| | 0 | 3393 | | } |
| | 0 | 3394 | | } |
| | | 3395 | | |
| | 0 | 3396 | | void ResolveIterator(IteratorDecl iter) { |
| | | 3397 | | Contract.Requires(iter != null); |
| | | 3398 | | Contract.Requires(currentClass == null); |
| | | 3399 | | Contract.Ensures(currentClass == null); |
| | | 3400 | | |
| | 0 | 3401 | | var initialErrorCount = reporter.Count(ErrorLevel.Error); |
| | | 3402 | | |
| | | 3403 | | // Add in-parameters to the scope, but don't care about any duplication errors, since they have already been repor |
| | 0 | 3404 | | scope.PushMarker(); |
| | 0 | 3405 | | scope.AllowInstance = false; // disallow 'this' from use, which means that the special fields and methods added a |
| | 0 | 3406 | | iter.Ins.ForEach(p => scope.Push(p.Name, p)); |
| | 0 | 3407 | | ResolveParameterDefaultValues(iter.Ins, new ResolutionContext(iter, false)); |
| | | 3408 | | |
| | | 3409 | | // Start resolving specification... |
| | | 3410 | | // we start with the decreases clause, because the _decreases<n> fields were only given type proxies before; we'll |
| | | 3411 | | // the types only after resolving the decreases clause (and it may be that some of resolution has already seen use |
| | | 3412 | | // these fields; so, with no further ado, here we go |
| | 0 | 3413 | | ResolveAttributes(iter.Decreases, new ResolutionContext(iter, false)); |
| | 0 | 3414 | | Contract.Assert(iter.Decreases.Expressions.Count == iter.DecreasesFields.Count); |
| | 0 | 3415 | | for (var i = 0; i < iter.Decreases.Expressions.Count; i++) { |
| | 0 | 3416 | | var e = iter.Decreases.Expressions[i]; |
| | 0 | 3417 | | ResolveExpression(e, new ResolutionContext(iter, false)); |
| | | 3418 | | // any type is fine, but associate this type with the corresponding _decreases<n> field |
| | 0 | 3419 | | var d = iter.DecreasesFields[i]; |
| | | 3420 | | // If the following type constraint does not hold, then: Bummer, there was a use--and a bad use--of the field be |
| | 0 | 3421 | | ConstrainSubtypeRelation(d.Type, e.Type, e, "type of field {0} is {1}, but has been constrained elsewhere to be |
| | 0 | 3422 | | } |
| | 0 | 3423 | | foreach (FrameExpression fe in iter.Reads.Expressions) { |
| | 0 | 3424 | | ResolveFrameExpressionTopLevel(fe, FrameExpressionUse.Reads, iter); |
| | 0 | 3425 | | } |
| | 0 | 3426 | | ResolveAttributes(iter.Modifies, new ResolutionContext(iter, false)); |
| | 0 | 3427 | | foreach (FrameExpression fe in iter.Modifies.Expressions) { |
| | 0 | 3428 | | ResolveFrameExpressionTopLevel(fe, FrameExpressionUse.Modifies, iter); |
| | 0 | 3429 | | } |
| | 0 | 3430 | | foreach (AttributedExpression e in iter.Requires) { |
| | 0 | 3431 | | ResolveAttributes(e, new ResolutionContext(iter, false)); |
| | 0 | 3432 | | ResolveExpression(e.E, new ResolutionContext(iter, false)); |
| | 0 | 3433 | | Contract.Assert(e.E.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 3434 | | ConstrainTypeExprBool(e.E, "Precondition must be a boolean (got {0})"); |
| | 0 | 3435 | | } |
| | | 3436 | | |
| | 0 | 3437 | | scope.PopMarker(); // for the in-parameters |
| | | 3438 | | |
| | | 3439 | | // We resolve the rest of the specification in an instance context. So mentions of the in- or yield-parameters |
| | | 3440 | | // get resolved as field dereferences (with an implicit "this") |
| | 0 | 3441 | | scope.PushMarker(); |
| | 0 | 3442 | | currentClass = iter; |
| | 0 | 3443 | | Contract.Assert(scope.AllowInstance); |
| | | 3444 | | |
| | 0 | 3445 | | foreach (AttributedExpression e in iter.YieldRequires) { |
| | 0 | 3446 | | ResolveAttributes(e, new ResolutionContext(iter, false)); |
| | 0 | 3447 | | ResolveExpression(e.E, new ResolutionContext(iter, false)); |
| | 0 | 3448 | | Contract.Assert(e.E.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 3449 | | ConstrainTypeExprBool(e.E, "Yield precondition must be a boolean (got {0})"); |
| | 0 | 3450 | | } |
| | 0 | 3451 | | foreach (AttributedExpression e in iter.YieldEnsures) { |
| | 0 | 3452 | | ResolveAttributes(e, new ResolutionContext(iter, true)); |
| | 0 | 3453 | | ResolveExpression(e.E, new ResolutionContext(iter, true)); |
| | 0 | 3454 | | Contract.Assert(e.E.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 3455 | | ConstrainTypeExprBool(e.E, "Yield postcondition must be a boolean (got {0})"); |
| | 0 | 3456 | | } |
| | 0 | 3457 | | foreach (AttributedExpression e in iter.Ensures) { |
| | 0 | 3458 | | ResolveAttributes(e, new ResolutionContext(iter, true)); |
| | 0 | 3459 | | ResolveExpression(e.E, new ResolutionContext(iter, true)); |
| | 0 | 3460 | | Contract.Assert(e.E.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 3461 | | ConstrainTypeExprBool(e.E, "Postcondition must be a boolean (got {0})"); |
| | 0 | 3462 | | } |
| | 0 | 3463 | | SolveAllTypeConstraints(); |
| | | 3464 | | |
| | 0 | 3465 | | var postSpecErrorCount = reporter.Count(ErrorLevel.Error); |
| | | 3466 | | |
| | | 3467 | | // Resolve body |
| | 0 | 3468 | | if (iter.Body != null) { |
| | 0 | 3469 | | DominatingStatementLabels.PushMarker(); |
| | 0 | 3470 | | foreach (var req in iter.Requires) { |
| | 0 | 3471 | | if (req.Label != null) { |
| | 0 | 3472 | | if (DominatingStatementLabels.Find(req.Label.Name) != null) { |
| | 0 | 3473 | | reporter.Error(MessageSource.Resolver, req.Label.Tok, "assert label shadows a dominating label"); |
| | 0 | 3474 | | } else { |
| | 0 | 3475 | | var rr = DominatingStatementLabels.Push(req.Label.Name, req.Label); |
| | 0 | 3476 | | Contract.Assert(rr == Scope<Label>.PushResult.Success); // since we just checked for duplicates, we expec |
| | 0 | 3477 | | } |
| | 0 | 3478 | | } |
| | 0 | 3479 | | } |
| | 0 | 3480 | | ResolveBlockStatement(iter.Body, ResolutionContext.FromCodeContext(iter)); |
| | 0 | 3481 | | DominatingStatementLabels.PopMarker(); |
| | 0 | 3482 | | SolveAllTypeConstraints(); |
| | 0 | 3483 | | } |
| | | 3484 | | |
| | 0 | 3485 | | currentClass = null; |
| | 0 | 3486 | | scope.PopMarker(); // pop off the AllowInstance setting |
| | | 3487 | | |
| | 0 | 3488 | | if (postSpecErrorCount == initialErrorCount) { |
| | 0 | 3489 | | iter.CreateIteratorMethodSpecs(this); |
| | 0 | 3490 | | } |
| | 0 | 3491 | | } |
| | | 3492 | | |
| | | 3493 | | /// <summary> |
| | | 3494 | | /// Checks if lhs, which is expected to be a successfully resolved expression, denotes something |
| | | 3495 | | /// that can be assigned to. In particular, this means that lhs denotes a mutable variable, field, |
| | | 3496 | | /// or array element. If a violation is detected, an error is reported. |
| | | 3497 | | /// </summary> |
| | 354600 | 3498 | | public void CheckIsLvalue(Expression lhs, ResolutionContext resolutionContext) { |
| | | 3499 | | Contract.Requires(lhs != null); |
| | | 3500 | | Contract.Requires(resolutionContext != null); |
| | 709200 | 3501 | | if (lhs is IdentifierExpr) { |
| | 354600 | 3502 | | var ll = (IdentifierExpr)lhs; |
| | 354600 | 3503 | | if (!ll.Var.IsMutable) { |
| | 0 | 3504 | | reporter.Error(MessageSource.Resolver, lhs, "LHS of assignment must denote a mutable variable"); |
| | 0 | 3505 | | } |
| | 354600 | 3506 | | } else if (lhs is MemberSelectExpr) { |
| | 0 | 3507 | | var ll = (MemberSelectExpr)lhs; |
| | 0 | 3508 | | var field = ll.Member as Field; |
| | 0 | 3509 | | if (field == null || !field.IsUserMutable) { |
| | 0 | 3510 | | if (resolutionContext.InFirstPhaseConstructor && field is ConstantField cf && !cf.IsStatic && cf.Rhs == null) |
| | 0 | 3511 | | if (Expression.AsThis(ll.Obj) != null) { |
| | | 3512 | | // it's cool; this field can be assigned to here |
| | 0 | 3513 | | } else { |
| | 0 | 3514 | | reporter.Error(MessageSource.Resolver, lhs, "LHS of assignment must denote a mutable field of 'this'"); |
| | 0 | 3515 | | } |
| | 0 | 3516 | | } else { |
| | 0 | 3517 | | reporter.Error(MessageSource.Resolver, lhs, "LHS of assignment must denote a mutable field"); |
| | 0 | 3518 | | } |
| | 0 | 3519 | | } |
| | 0 | 3520 | | } else if (lhs is SeqSelectExpr) { |
| | 0 | 3521 | | var ll = (SeqSelectExpr)lhs; |
| | 0 | 3522 | | ConstrainSubtypeRelation(ResolvedArrayType(ll.Seq.tok, 1, new InferredTypeProxy(), resolutionContext, true), ll. |
| | 0 | 3523 | | "LHS of array assignment must denote an array element (found {0})", ll.Seq.Type); |
| | 0 | 3524 | | if (!ll.SelectOne) { |
| | 0 | 3525 | | reporter.Error(MessageSource.Resolver, ll.Seq, "cannot assign to a range of array elements (try the 'forall' s |
| | 0 | 3526 | | } |
| | 0 | 3527 | | } else if (lhs is MultiSelectExpr) { |
| | | 3528 | | // nothing to check; this can only denote an array element |
| | 0 | 3529 | | } else { |
| | 0 | 3530 | | reporter.Error(MessageSource.Resolver, lhs, "LHS of assignment must denote a mutable variable or field"); |
| | 0 | 3531 | | } |
| | 354600 | 3532 | | } |
| | | 3533 | | |
| | 34250 | 3534 | | public void ResolveBlockStatement(BlockStmt blockStmt, ResolutionContext resolutionContext) { |
| | | 3535 | | Contract.Requires(blockStmt != null); |
| | | 3536 | | Contract.Requires(resolutionContext != null); |
| | | 3537 | | |
| | 34250 | 3538 | | if (blockStmt is DividedBlockStmt) { |
| | 0 | 3539 | | var div = (DividedBlockStmt)blockStmt; |
| | 0 | 3540 | | Contract.Assert(currentMethod is Constructor); // divided bodies occur only in class constructors |
| | 0 | 3541 | | Contract.Assert(!resolutionContext.InFirstPhaseConstructor); // divided bodies are never nested |
| | 0 | 3542 | | foreach (Statement ss in div.BodyInit) { |
| | 0 | 3543 | | ResolveStatementWithLabels(ss, resolutionContext with { InFirstPhaseConstructor = true }); |
| | 0 | 3544 | | } |
| | 0 | 3545 | | foreach (Statement ss in div.BodyProper) { |
| | 0 | 3546 | | ResolveStatementWithLabels(ss, resolutionContext); |
| | 0 | 3547 | | } |
| | 34250 | 3548 | | } else { |
| | 1332240 | 3549 | | foreach (Statement ss in blockStmt.Body) { |
| | 409830 | 3550 | | ResolveStatementWithLabels(ss, resolutionContext); |
| | 409830 | 3551 | | } |
| | 34250 | 3552 | | } |
| | 34250 | 3553 | | } |
| | | 3554 | | |
| | 409830 | 3555 | | public void ResolveStatementWithLabels(Statement stmt, ResolutionContext resolutionContext) { |
| | | 3556 | | Contract.Requires(stmt != null); |
| | | 3557 | | Contract.Requires(resolutionContext != null); |
| | | 3558 | | |
| | 409830 | 3559 | | enclosingStatementLabels.PushMarker(); |
| | | 3560 | | // push labels |
| | 819660 | 3561 | | for (var l = stmt.Labels; l != null; l = l.Next) { |
| | 0 | 3562 | | var lnode = l.Data; |
| | 0 | 3563 | | Contract.Assert(lnode.Name != null); // LabelNode's with .Label==null are added only during resolution of the b |
| | 0 | 3564 | | var prev = enclosingStatementLabels.Find(lnode.Name); |
| | 0 | 3565 | | if (prev == stmt) { |
| | 0 | 3566 | | reporter.Error(MessageSource.Resolver, lnode.Tok, "duplicate label"); |
| | 0 | 3567 | | } else if (prev != null) { |
| | 0 | 3568 | | reporter.Error(MessageSource.Resolver, lnode.Tok, "label shadows an enclosing label"); |
| | 0 | 3569 | | } else { |
| | 0 | 3570 | | var r = enclosingStatementLabels.Push(lnode.Name, stmt); |
| | 0 | 3571 | | Contract.Assert(r == Scope<Statement>.PushResult.Success); // since we just checked for duplicates, we expect |
| | 0 | 3572 | | if (DominatingStatementLabels.Find(lnode.Name) != null) { |
| | 0 | 3573 | | reporter.Error(MessageSource.Resolver, lnode.Tok, "label shadows a dominating label"); |
| | 0 | 3574 | | } else { |
| | 0 | 3575 | | var rr = DominatingStatementLabels.Push(lnode.Name, lnode); |
| | 0 | 3576 | | Contract.Assert(rr == Scope<Label>.PushResult.Success); // since we just checked for duplicates, we expect |
| | 0 | 3577 | | } |
| | 0 | 3578 | | } |
| | 0 | 3579 | | } |
| | 409830 | 3580 | | ResolveStatement(stmt, resolutionContext); |
| | 409830 | 3581 | | enclosingStatementLabels.PopMarker(); |
| | 409830 | 3582 | | } |
| | | 3583 | | |
| | 0 | 3584 | | void ResolveAlternatives(List<GuardedAlternative> alternatives, AlternativeLoopStmt loopToCatchBreaks, ResolutionCon |
| | | 3585 | | Contract.Requires(alternatives != null); |
| | | 3586 | | Contract.Requires(resolutionContext != null); |
| | | 3587 | | |
| | | 3588 | | // first, resolve the guards |
| | 0 | 3589 | | foreach (var alternative in alternatives) { |
| | 0 | 3590 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 3591 | | ResolveExpression(alternative.Guard, resolutionContext); |
| | 0 | 3592 | | Contract.Assert(alternative.Guard.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 3593 | | bool successfullyResolved = reporter.Count(ErrorLevel.Error) == prevErrorCount; |
| | 0 | 3594 | | ConstrainTypeExprBool(alternative.Guard, "condition is expected to be of type bool, but is {0}"); |
| | 0 | 3595 | | } |
| | | 3596 | | |
| | 0 | 3597 | | if (loopToCatchBreaks != null) { |
| | 0 | 3598 | | loopStack.Add(loopToCatchBreaks); // push |
| | 0 | 3599 | | } |
| | 0 | 3600 | | foreach (var alternative in alternatives) { |
| | 0 | 3601 | | scope.PushMarker(); |
| | 0 | 3602 | | DominatingStatementLabels.PushMarker(); |
| | 0 | 3603 | | if (alternative.IsBindingGuard) { |
| | 0 | 3604 | | var exists = (ExistsExpr)alternative.Guard; |
| | 0 | 3605 | | foreach (var v in exists.BoundVars) { |
| | 0 | 3606 | | ScopePushAndReport(scope, v, "bound-variable"); |
| | 0 | 3607 | | } |
| | 0 | 3608 | | } |
| | 0 | 3609 | | ResolveAttributes(alternative, resolutionContext); |
| | 0 | 3610 | | foreach (Statement ss in alternative.Body) { |
| | 0 | 3611 | | ResolveStatementWithLabels(ss, resolutionContext); |
| | 0 | 3612 | | } |
| | 0 | 3613 | | DominatingStatementLabels.PopMarker(); |
| | 0 | 3614 | | scope.PopMarker(); |
| | 0 | 3615 | | } |
| | 0 | 3616 | | if (loopToCatchBreaks != null) { |
| | 0 | 3617 | | loopStack.RemoveAt(loopStack.Count - 1); // pop |
| | 0 | 3618 | | } |
| | 0 | 3619 | | } |
| | | 3620 | | |
| | | 3621 | | /// <summary> |
| | | 3622 | | /// Resolves the given call statement. |
| | | 3623 | | /// Assumes all LHSs have already been resolved (and checked for mutability). |
| | | 3624 | | /// </summary> |
| | 10120 | 3625 | | void ResolveCallStmt(CallStmt s, ResolutionContext resolutionContext, Type receiverType) { |
| | | 3626 | | Contract.Requires(s != null); |
| | | 3627 | | Contract.Requires(resolutionContext != null); |
| | 10120 | 3628 | | bool isInitCall = receiverType != null; |
| | | 3629 | | |
| | 10120 | 3630 | | var callee = s.Method; |
| | 10120 | 3631 | | Contract.Assert(callee != null); // follows from the invariant of CallStmt |
| | 10120 | 3632 | | if (!isInitCall && callee is Constructor) { |
| | 0 | 3633 | | reporter.Error(MessageSource.Resolver, s, "a constructor is allowed to be called only when an object is being al |
| | 0 | 3634 | | } |
| | | 3635 | | |
| | | 3636 | | // resolve left-hand sides (the right-hand sides are resolved below) |
| | 75450 | 3637 | | foreach (var lhs in s.Lhs) { |
| | 15030 | 3638 | | Contract.Assume(lhs.Type != null); // a sanity check that LHSs have already been resolved |
| | 15030 | 3639 | | } |
| | | 3640 | | |
| | 10120 | 3641 | | bool tryToResolve = false; |
| | 10120 | 3642 | | if (callee.Outs.Count != s.Lhs.Count) { |
| | 0 | 3643 | | if (isInitCall) { |
| | 0 | 3644 | | reporter.Error(MessageSource.Resolver, s, "a method called as an initialization method must not have any resul |
| | 0 | 3645 | | } else { |
| | 0 | 3646 | | reporter.Error(MessageSource.Resolver, s, "wrong number of method result arguments (got {0}, expected {1})", s |
| | 0 | 3647 | | tryToResolve = true; |
| | 0 | 3648 | | } |
| | 10120 | 3649 | | } else { |
| | 10120 | 3650 | | if (isInitCall) { |
| | 0 | 3651 | | if (callee.IsStatic) { |
| | 0 | 3652 | | reporter.Error(MessageSource.Resolver, s.Tok, "a method called as an initialization method must not be 'stat |
| | 0 | 3653 | | } else { |
| | 0 | 3654 | | tryToResolve = true; |
| | 0 | 3655 | | } |
| | 10120 | 3656 | | } else if (!callee.IsStatic) { |
| | 0 | 3657 | | if (!scope.AllowInstance && s.Receiver is ThisExpr) { |
| | | 3658 | | // The call really needs an instance, but that instance is given as 'this', which is not |
| | | 3659 | | // available in this context. For more details, see comment in the resolution of a |
| | | 3660 | | // FunctionCallExpr. |
| | 0 | 3661 | | reporter.Error(MessageSource.Resolver, s.Receiver, "'this' is not allowed in a 'static' context"); |
| | 0 | 3662 | | } else if (s.Receiver is StaticReceiverExpr) { |
| | 0 | 3663 | | reporter.Error(MessageSource.Resolver, s.Receiver, "call to instance method requires an instance"); |
| | 0 | 3664 | | } else { |
| | 0 | 3665 | | tryToResolve = true; |
| | 0 | 3666 | | } |
| | 10120 | 3667 | | } else { |
| | 10120 | 3668 | | tryToResolve = true; |
| | 10120 | 3669 | | } |
| | 10120 | 3670 | | } |
| | | 3671 | | |
| | 20240 | 3672 | | if (tryToResolve) { |
| | 10120 | 3673 | | var typeMap = s.MethodSelect.TypeArgumentSubstitutionsAtMemberDeclaration(); |
| | | 3674 | | // resolve arguments |
| | 10120 | 3675 | | ResolveActualParameters(s.Bindings, callee.Ins, s.Tok, callee, resolutionContext, typeMap, |
| | 10120 | 3676 | | callee.IsStatic ? null : s.Receiver); |
| | | 3677 | | // type check the out-parameter arguments (in-parameters were type checked as part of ResolveActualParameters) |
| | 65330 | 3678 | | for (int i = 0; i < callee.Outs.Count && i < s.Lhs.Count; i++) { |
| | 15030 | 3679 | | var outFormal = callee.Outs[i]; |
| | 15030 | 3680 | | var it = outFormal.Type; |
| | 15030 | 3681 | | Type st = it.Subst(typeMap); |
| | 15030 | 3682 | | var lhs = s.Lhs[i]; |
| | 15030 | 3683 | | var what = GetLocationInformation(outFormal, callee.Outs.Count(), i, "method out-parameter"); |
| | | 3684 | | |
| | 15030 | 3685 | | AddAssignableConstraint( |
| | 15030 | 3686 | | s.Tok, lhs.Type, st, |
| | 15030 | 3687 | | $"incorrect return type {what} (expected {{1}}, got {{0}})"); |
| | 15030 | 3688 | | } |
| | 65330 | 3689 | | for (int i = 0; i < s.Lhs.Count; i++) { |
| | 15030 | 3690 | | var lhs = s.Lhs[i]; |
| | | 3691 | | // LHS must denote a mutable field. |
| | 15030 | 3692 | | CheckIsLvalue(lhs.Resolved, resolutionContext); |
| | 15030 | 3693 | | } |
| | 10120 | 3694 | | } |
| | 10120 | 3695 | | if (Contract.Exists(callee.Decreases.Expressions, e => e is WildcardExpr) && !resolutionContext.CodeContext.Allows |
| | 0 | 3696 | | reporter.Error(MessageSource.Resolver, s.Tok, "a call to a possibly non-terminating method is allowed only if th |
| | 0 | 3697 | | } |
| | 10120 | 3698 | | } |
| | | 3699 | | |
| | | 3700 | | /// <summary> |
| | | 3701 | | /// Resolve the actual arguments given in "bindings". Then, check that there is exactly one |
| | | 3702 | | /// actual for each formal, and impose assignable constraints. |
| | | 3703 | | /// "typeMap" is applied to the type of each formal. |
| | | 3704 | | /// This method should be called only once. That is, bindings.arguments is required to be null on entry to this meth |
| | | 3705 | | /// </summary> |
| | | 3706 | | void ResolveActualParameters(ActualBindings bindings, List<Formal> formals, IToken callTok, object context, Resoluti |
| | 160800 | 3707 | | Dictionary<TypeParameter, Type> typeMap, Expression/*?*/ receiver) { |
| | | 3708 | | Contract.Requires(bindings != null); |
| | | 3709 | | Contract.Requires(formals != null); |
| | | 3710 | | Contract.Requires(callTok != null); |
| | | 3711 | | Contract.Requires(context is Method || context is Function || context is DatatypeCtor || context is ArrowType); |
| | | 3712 | | Contract.Requires(typeMap != null); |
| | | 3713 | | Contract.Requires(!bindings.WasResolved); |
| | | 3714 | | |
| | | 3715 | | string whatKind; |
| | | 3716 | | string name; |
| | 170920 | 3717 | | if (context is Method cMethod) { |
| | 10120 | 3718 | | whatKind = cMethod.WhatKind; |
| | 10120 | 3719 | | name = $"{whatKind} '{cMethod.Name}'"; |
| | 167820 | 3720 | | } else if (context is Function cFunction) { |
| | 7020 | 3721 | | whatKind = cFunction.WhatKind; |
| | 7020 | 3722 | | name = $"{whatKind} '{cFunction.Name}'"; |
| | 293820 | 3723 | | } else if (context is DatatypeCtor cCtor) { |
| | 143140 | 3724 | | whatKind = "datatype constructor"; |
| | 143140 | 3725 | | name = $"{whatKind} '{cCtor.Name}'"; |
| | 143660 | 3726 | | } else { |
| | 520 | 3727 | | var cArrowType = (ArrowType)context; |
| | 520 | 3728 | | whatKind = "function application"; |
| | 520 | 3729 | | name = $"function type '{cArrowType}'"; |
| | 520 | 3730 | | } |
| | | 3731 | | |
| | | 3732 | | // If all arguments are passed positionally, use simple error messages that talk about the count of arguments. |
| | 469430 | 3733 | | var onlyPositionalArguments = bindings.ArgumentBindings.TrueForAll(binding => binding.FormalParameterName == null) |
| | 160800 | 3734 | | var simpleErrorReported = false; |
| | 321600 | 3735 | | if (onlyPositionalArguments) { |
| | 469430 | 3736 | | var requiredParametersCount = formals.Count(f => f.DefaultValue == null); |
| | 160800 | 3737 | | var actualsCounts = bindings.ArgumentBindings.Count; |
| | 160800 | 3738 | | var sig = ""; |
| | 1247490 | 3739 | | for (int i = 0; i < formals.Count; i++) { |
| | 308630 | 3740 | | sig += (", " + formals[i].Name + ": " + formals[i].Type.ToString()); |
| | 308630 | 3741 | | } |
| | 280860 | 3742 | | if (formals.Count > 0) { |
| | 120060 | 3743 | | sig = ": (" + sig[2..] + ")"; |
| | 120060 | 3744 | | } |
| | 321600 | 3745 | | if (requiredParametersCount <= actualsCounts && actualsCounts <= formals.Count) { |
| | | 3746 | | // the situation is plausible |
| | 160800 | 3747 | | } else if (requiredParametersCount == formals.Count) { |
| | | 3748 | | // this is the common, classical case of no default parameter values; generate a straightforward error message |
| | 0 | 3749 | | reporter.Error(MessageSource.Resolver, callTok, $"wrong number of arguments (got {actualsCounts}, but {name} e |
| | 0 | 3750 | | simpleErrorReported = true; |
| | 0 | 3751 | | } else if (actualsCounts < requiredParametersCount) { |
| | 0 | 3752 | | reporter.Error(MessageSource.Resolver, callTok, $"wrong number of arguments (got {actualsCounts}, but {name} e |
| | 0 | 3753 | | simpleErrorReported = true; |
| | 0 | 3754 | | } else { |
| | 0 | 3755 | | reporter.Error(MessageSource.Resolver, callTok, $"wrong number of arguments (got {actualsCounts}, but {name} e |
| | 0 | 3756 | | simpleErrorReported = true; |
| | 0 | 3757 | | } |
| | 160800 | 3758 | | } |
| | | 3759 | | |
| | | 3760 | | // resolve given arguments and populate the "namesToActuals" map |
| | 160800 | 3761 | | var namesToActuals = new Dictionary<string, ActualBinding>(); |
| | 469430 | 3762 | | formals.ForEach(f => namesToActuals.Add(f.Name, null)); // a name mapping to "null" says it hasn't been filled in |
| | 160800 | 3763 | | var stillAcceptingPositionalArguments = true; |
| | 160800 | 3764 | | var bindingIndex = 0; |
| | 1408290 | 3765 | | foreach (var binding in bindings.ArgumentBindings) { |
| | 308630 | 3766 | | var arg = binding.Actual; |
| | | 3767 | | // insert the actual into "namesToActuals" under an appropriate name, unless there is an error |
| | 308630 | 3768 | | if (binding.FormalParameterName != null) { |
| | 0 | 3769 | | var pname = binding.FormalParameterName.val; |
| | 0 | 3770 | | stillAcceptingPositionalArguments = false; |
| | 0 | 3771 | | if (!namesToActuals.TryGetValue(pname, out var b)) { |
| | 0 | 3772 | | reporter.Error(MessageSource.Resolver, binding.FormalParameterName, $"the binding named '{pname}' does not c |
| | 0 | 3773 | | } else if (b == null) { |
| | | 3774 | | // all is good |
| | 0 | 3775 | | namesToActuals[pname] = binding; |
| | 0 | 3776 | | } else if (b.FormalParameterName == null) { |
| | 0 | 3777 | | reporter.Error(MessageSource.Resolver, binding.FormalParameterName, $"the parameter named '{pname}' is alrea |
| | 0 | 3778 | | } else { |
| | 0 | 3779 | | reporter.Error(MessageSource.Resolver, binding.FormalParameterName, $"duplicate binding for parameter name ' |
| | 0 | 3780 | | } |
| | 308630 | 3781 | | } else if (!stillAcceptingPositionalArguments) { |
| | 0 | 3782 | | reporter.Error(MessageSource.Resolver, arg.tok, "a positional argument is not allowed to follow named argument |
| | 617260 | 3783 | | } else if (bindingIndex < formals.Count) { |
| | | 3784 | | // use the name of formal corresponding to this positional argument, unless the parameter is named-only |
| | 308630 | 3785 | | var formal = formals[bindingIndex]; |
| | 308630 | 3786 | | var pname = formal.Name; |
| | 308630 | 3787 | | if (formal.IsNameOnly) { |
| | 0 | 3788 | | reporter.Error(MessageSource.Resolver, arg.tok, |
| | 0 | 3789 | | $"nameonly parameter '{pname}' must be passed using a name binding; it cannot be passed positionally"); |
| | 0 | 3790 | | } |
| | 308630 | 3791 | | Contract.Assert(namesToActuals[pname] == null); // we expect this, since we've only filled parameters position |
| | 308630 | 3792 | | namesToActuals[pname] = binding; |
| | 308630 | 3793 | | } else { |
| | | 3794 | | // too many positional arguments |
| | 0 | 3795 | | if (onlyPositionalArguments) { |
| | | 3796 | | // error was reported before the "foreach" loop |
| | 0 | 3797 | | Contract.Assert(simpleErrorReported); |
| | 0 | 3798 | | } else if (formals.Count < bindingIndex) { |
| | | 3799 | | // error was reported on a previous iteration of this "foreach" loop |
| | 0 | 3800 | | } else { |
| | 0 | 3801 | | reporter.Error(MessageSource.Resolver, callTok, |
| | 0 | 3802 | | $"wrong number of arguments ({name} expects {formals.Count}, got {bindings.ArgumentBindings.Count})"); |
| | 0 | 3803 | | } |
| | 0 | 3804 | | } |
| | | 3805 | | |
| | | 3806 | | // resolve argument |
| | 308630 | 3807 | | ResolveExpression(arg, resolutionContext); |
| | 308630 | 3808 | | bindingIndex++; |
| | 308630 | 3809 | | } |
| | | 3810 | | |
| | 160800 | 3811 | | var actuals = new List<Expression>(); |
| | 160800 | 3812 | | var formalIndex = 0; |
| | 160800 | 3813 | | var substMap = new Dictionary<IVariable, Expression>(); |
| | 1408290 | 3814 | | foreach (var formal in formals) { |
| | 308630 | 3815 | | var b = namesToActuals[formal.Name]; |
| | 617260 | 3816 | | if (b != null) { |
| | 308630 | 3817 | | actuals.Add(b.Actual); |
| | 308630 | 3818 | | substMap.Add(formal, b.Actual); |
| | 308630 | 3819 | | var what = GetLocationInformation(formal, |
| | 308630 | 3820 | | bindings.ArgumentBindings.Count(), bindings.ArgumentBindings.IndexOf(b), |
| | 308630 | 3821 | | whatKind + (context is Method ? " in-parameter" : " parameter")); |
| | | 3822 | | |
| | 308630 | 3823 | | AddAssignableConstraint( |
| | 308630 | 3824 | | callTok, formal.Type.Subst(typeMap), b.Actual.Type, |
| | 308630 | 3825 | | $"incorrect argument type {what} (expected {{0}}, found {{1}})"); |
| | 308630 | 3826 | | } else if (formal.DefaultValue != null) { |
| | | 3827 | | // Note, in the following line, "substMap" is passed in, but it hasn't been fully filled in until the |
| | | 3828 | | // end of this foreach loop. Still, that's soon enough, because DefaultValueExpression won't use it |
| | | 3829 | | // until FillInDefaultValueExpressions at the end of Pass 1 of the Resolver. |
| | 0 | 3830 | | var n = new DefaultValueExpression(callTok, formal, receiver, substMap, typeMap); |
| | 0 | 3831 | | allDefaultValueExpressions.Add(n); |
| | 0 | 3832 | | actuals.Add(n); |
| | 0 | 3833 | | substMap.Add(formal, n); |
| | 0 | 3834 | | } else { |
| | | 3835 | | // parameter has no value |
| | 0 | 3836 | | if (onlyPositionalArguments) { |
| | | 3837 | | // a simple error message has already been reported |
| | 0 | 3838 | | Contract.Assert(simpleErrorReported); |
| | 0 | 3839 | | } else { |
| | 0 | 3840 | | var formalDescription = whatKind + (context is Method ? " in-parameter" : " parameter"); |
| | 0 | 3841 | | var nameWithIndex = formal.HasName && formal is not ImplicitFormal ? "'" + formal.Name + "'" : ""; |
| | 0 | 3842 | | if (formals.Count > 1 || nameWithIndex == "") { |
| | 0 | 3843 | | nameWithIndex += nameWithIndex == "" ? "" : " "; |
| | 0 | 3844 | | nameWithIndex += $"at index {formalIndex}"; |
| | 0 | 3845 | | } |
| | 0 | 3846 | | var message = $"{formalDescription} {nameWithIndex} requires an argument of type {formal.Type}"; |
| | 0 | 3847 | | reporter.Error(MessageSource.Resolver, callTok, message); |
| | 0 | 3848 | | } |
| | 0 | 3849 | | } |
| | 308630 | 3850 | | formalIndex++; |
| | 308630 | 3851 | | } |
| | | 3852 | | |
| | 160800 | 3853 | | bindings.AcceptArgumentExpressionsAsExactParameterList(actuals); |
| | 160800 | 3854 | | } |
| | | 3855 | | |
| | 323660 | 3856 | | private static string GetLocationInformation(Formal parameter, int bindingCount, int bindingIndex, string formalDesc |
| | 323660 | 3857 | | var displayName = parameter.HasName && parameter is not ImplicitFormal; |
| | 323660 | 3858 | | var description = ""; |
| | 639690 | 3859 | | if (bindingCount > 1) { |
| | 316030 | 3860 | | description += $"at index {bindingIndex} "; |
| | 316030 | 3861 | | } |
| | | 3862 | | |
| | 323660 | 3863 | | description += $"for {formalDescription}"; |
| | | 3864 | | |
| | 645980 | 3865 | | if (displayName) { |
| | 322320 | 3866 | | description += $" '{parameter.Name}'"; |
| | 322320 | 3867 | | } |
| | | 3868 | | |
| | 323660 | 3869 | | return description; |
| | 323660 | 3870 | | } |
| | | 3871 | | |
| | | 3872 | | /// <summary> |
| | | 3873 | | /// To resolve "id" in expression "E . id", do: |
| | | 3874 | | /// * If E denotes a module name M: |
| | | 3875 | | /// 0. Member of module M: sub-module (including submodules of imports), class, datatype, etc. |
| | | 3876 | | /// (if two imported types have the same name, an error message is produced here) |
| | | 3877 | | /// 1. Static member of M._default denoting an async task type |
| | | 3878 | | /// (Note that in contrast to ResolveNameSegment_Type, imported modules, etc. are ignored) |
| | | 3879 | | /// * If E denotes a type: |
| | | 3880 | | /// 2. a. Member of that type denoting an async task type, or: |
| | | 3881 | | /// b. If allowDanglingDotName: |
| | | 3882 | | /// Return the type "E" and the given "expr", letting the caller try to make sense of the final dot-name. |
| | | 3883 | | /// |
| | | 3884 | | /// Note: 1 and 2a are not used now, but they will be of interest when async task types are supported. |
| | | 3885 | | /// </summary> |
| | 0 | 3886 | | ResolveTypeReturn ResolveDotSuffix_Type(ExprDotName expr, ResolutionContext resolutionContext, bool allowDanglingDot |
| | | 3887 | | Contract.Requires(expr != null); |
| | | 3888 | | Contract.Requires(!expr.WasResolved()); |
| | | 3889 | | Contract.Requires(expr.Lhs is NameSegment || expr.Lhs is ExprDotName); |
| | | 3890 | | Contract.Requires(resolutionContext != null); |
| | | 3891 | | Contract.Ensures(Contract.Result<ResolveTypeReturn>() == null || allowDanglingDotName); |
| | | 3892 | | |
| | | 3893 | | // resolve the LHS expression |
| | 0 | 3894 | | if (expr.Lhs is NameSegment) { |
| | 0 | 3895 | | ResolveNameSegment_Type((NameSegment)expr.Lhs, resolutionContext, option, defaultTypeArguments); |
| | 0 | 3896 | | } else { |
| | 0 | 3897 | | ResolveDotSuffix_Type((ExprDotName)expr.Lhs, resolutionContext, false, option, defaultTypeArguments); |
| | 0 | 3898 | | } |
| | | 3899 | | |
| | 0 | 3900 | | if (expr.OptTypeArguments != null) { |
| | 0 | 3901 | | foreach (var ty in expr.OptTypeArguments) { |
| | 0 | 3902 | | ResolveType(expr.tok, ty, resolutionContext, option, defaultTypeArguments); |
| | 0 | 3903 | | } |
| | 0 | 3904 | | } |
| | | 3905 | | |
| | 0 | 3906 | | Expression r = null; // the resolved expression, if successful |
| | | 3907 | | |
| | 0 | 3908 | | var lhs = expr.Lhs.Resolved; |
| | 0 | 3909 | | if (lhs != null && lhs.Type is Resolver_IdentifierExpr.ResolverType_Module) { |
| | 0 | 3910 | | var ri = (Resolver_IdentifierExpr)lhs; |
| | 0 | 3911 | | var sig = ((ModuleDecl)ri.Decl).AccessibleSignature(useCompileSignatures); |
| | 0 | 3912 | | sig = GetSignature(sig); |
| | | 3913 | | // For 0: |
| | | 3914 | | |
| | 0 | 3915 | | if (sig.TopLevels.TryGetValue(expr.SuffixName, out var decl)) { |
| | | 3916 | | // ----- 0. Member of the specified module |
| | 0 | 3917 | | if (decl is AmbiguousTopLevelDecl) { |
| | 0 | 3918 | | var ad = (AmbiguousTopLevelDecl)decl; |
| | 0 | 3919 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a type in one of the mo |
| | 0 | 3920 | | } else { |
| | | 3921 | | // We have found a module name or a type name. We create a temporary expression that will never be seen by |
| | | 3922 | | // or verifier, just to have a placeholder where we can recorded what we have found. |
| | 0 | 3923 | | r = CreateResolver_IdentifierExpr(expr.tok, expr.SuffixName, expr.OptTypeArguments, decl); |
| | 0 | 3924 | | } |
| | | 3925 | | #if ASYNC_TASK_TYPES |
| | | 3926 | | } else if (sig.StaticMembers.TryGetValue(expr.SuffixName, out member)) { |
| | | 3927 | | // ----- 1. static member of the specified module |
| | | 3928 | | Contract.Assert(member.IsStatic); // moduleInfo.StaticMembers is supposed to contain only static members of th |
| | | 3929 | | if (ReallyAmbiguousThing(ref member)) { |
| | | 3930 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a static member in one |
| | | 3931 | | } else { |
| | | 3932 | | var receiver = new StaticReceiverExpr(expr.tok, (ClassDecl)member.EnclosingClass); |
| | | 3933 | | r = ResolveExprDotCall(expr.tok, receiver, member, expr.OptTypeArguments, opts.resolutionContext, allowMetho |
| | | 3934 | | } |
| | | 3935 | | #endif |
| | 0 | 3936 | | } else { |
| | 0 | 3937 | | reporter.Error(MessageSource.Resolver, expr.tok, "module '{0}' does not declare a type '{1}'", ri.Decl.Name, e |
| | 0 | 3938 | | } |
| | | 3939 | | |
| | 0 | 3940 | | } else if (lhs != null && lhs.Type is Resolver_IdentifierExpr.ResolverType_Type) { |
| | 0 | 3941 | | var ri = (Resolver_IdentifierExpr)lhs; |
| | | 3942 | | // ----- 2. Look up name in type |
| | 0 | 3943 | | var ty = new UserDefinedType(ri.tok, ri.Decl.Name, ri.Decl, ri.TypeArgs); |
| | 0 | 3944 | | if (allowDanglingDotName && ty.IsRefType) { |
| | 0 | 3945 | | return new ResolveTypeReturn(ty, expr); |
| | | 3946 | | } |
| | 0 | 3947 | | if (r == null) { |
| | 0 | 3948 | | reporter.Error(MessageSource.Resolver, expr.tok, "member '{0}' does not exist in type '{1}' or cannot be part |
| | 0 | 3949 | | } |
| | 0 | 3950 | | } |
| | | 3951 | | |
| | 0 | 3952 | | if (r == null) { |
| | | 3953 | | // an error has been reported above; we won't fill in .ResolvedExpression, but we still must fill in .Type |
| | 0 | 3954 | | expr.Type = new InferredTypeProxy(); |
| | 0 | 3955 | | } else { |
| | 0 | 3956 | | expr.ResolvedExpression = r; |
| | 0 | 3957 | | expr.Type = r.Type; |
| | 0 | 3958 | | } |
| | 0 | 3959 | | return null; |
| | 0 | 3960 | | } |
| | | 3961 | | |
| | 94870 | 3962 | | internal Resolver_IdentifierExpr CreateResolver_IdentifierExpr(IToken tok, string name, List<Type> optTypeArguments, |
| | | 3963 | | Contract.Requires(tok != null); |
| | | 3964 | | Contract.Requires(name != null); |
| | | 3965 | | Contract.Requires(decl != null); |
| | | 3966 | | Contract.Ensures(Contract.Result<Resolver_IdentifierExpr>() != null); |
| | | 3967 | | |
| | 189740 | 3968 | | if (!moduleInfo.IsAbstract) { |
| | 94870 | 3969 | | if (decl is ModuleDecl md && md.Signature.IsAbstract) { |
| | 0 | 3970 | | reporter.Error(MessageSource.Resolver, tok, "a compiled module is not allowed to use an abstract module ({0})" |
| | 0 | 3971 | | } |
| | 94870 | 3972 | | } |
| | 94870 | 3973 | | var n = optTypeArguments == null ? 0 : optTypeArguments.Count; |
| | 118480 | 3974 | | if (optTypeArguments != null) { |
| | | 3975 | | // type arguments were supplied; they must be equal in number to those expected |
| | 23610 | 3976 | | if (n != decl.TypeArgs.Count) { |
| | 0 | 3977 | | reporter.Error(MessageSource.Resolver, tok, "Wrong number of type arguments ({0} instead of {1}) passed to {2} |
| | 0 | 3978 | | } |
| | 23610 | 3979 | | } |
| | 94870 | 3980 | | List<Type> tpArgs = new List<Type>(); |
| | 366950 | 3981 | | for (int i = 0; i < decl.TypeArgs.Count; i++) { |
| | 59070 | 3982 | | tpArgs.Add(i < n ? optTypeArguments[i] : new InferredTypeProxy()); |
| | 59070 | 3983 | | } |
| | 94870 | 3984 | | return new Resolver_IdentifierExpr(tok, decl, tpArgs); |
| | 94870 | 3985 | | } |
| | | 3986 | | |
| | 764940 | 3987 | | public void ResolveStatement(Statement stmt, ResolutionContext resolutionContext) { |
| | | 3988 | | Contract.Requires(stmt != null); |
| | | 3989 | | Contract.Requires(resolutionContext != null); |
| | 805180 | 3990 | | if (stmt is ICanResolve canResolve) { |
| | 40240 | 3991 | | canResolve.Resolve(this, resolutionContext); |
| | 40240 | 3992 | | return; |
| | | 3993 | | } |
| | 1449400 | 3994 | | if (!(stmt is ForallStmt || stmt is ForLoopStmt)) { // "forall" and "for" statements do their own attribute resol |
| | 724700 | 3995 | | ResolveAttributes(stmt, resolutionContext); |
| | 724700 | 3996 | | } |
| | 724700 | 3997 | | if (stmt is PredicateStmt) { |
| | 0 | 3998 | | PredicateStmt s = (PredicateStmt)stmt; |
| | 0 | 3999 | | var assertStmt = stmt as AssertStmt; |
| | 0 | 4000 | | if (assertStmt != null && assertStmt.Label != null) { |
| | 0 | 4001 | | if (DominatingStatementLabels.Find(assertStmt.Label.Name) != null) { |
| | 0 | 4002 | | reporter.Error(MessageSource.Resolver, assertStmt.Label.Tok, "assert label shadows a dominating label"); |
| | 0 | 4003 | | } else { |
| | 0 | 4004 | | var rr = DominatingStatementLabels.Push(assertStmt.Label.Name, assertStmt.Label); |
| | 0 | 4005 | | Contract.Assert(rr == Scope<Label>.PushResult.Success); // since we just checked for duplicates, we expect |
| | 0 | 4006 | | } |
| | 0 | 4007 | | } |
| | | 4008 | | |
| | 0 | 4009 | | if (assertStmt != null && Attributes.Find(assertStmt.Attributes, "only") is UserSuppliedAttributes attribute) { |
| | 0 | 4010 | | reporter.Warning(MessageSource.Verifier, ResolutionErrors.ErrorId.r_assert_only_assumes_others.ToString(), att |
| | 0 | 4011 | | "Assertion with {:only} temporarily transforms other assertions into assumptions"); |
| | 0 | 4012 | | if (attribute.Args.Count >= 1 |
| | 0 | 4013 | | && attribute.Args[0] is LiteralExpr { Value: string value } |
| | 0 | 4014 | | && value != "before" && value != "after") { |
| | 0 | 4015 | | reporter.Warning(MessageSource.Verifier, ResolutionErrors.ErrorId.r_assert_only_before_after.ToString(), att |
| | 0 | 4016 | | "{:only} only accepts \"before\" or \"after\" as an optional argument"); |
| | 0 | 4017 | | } |
| | 0 | 4018 | | } |
| | 0 | 4019 | | ResolveExpression(s.Expr, resolutionContext); |
| | 0 | 4020 | | Contract.Assert(s.Expr.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 4021 | | ConstrainTypeExprBool(s.Expr, "condition is expected to be of type bool, but is {0}"); |
| | 0 | 4022 | | if (assertStmt != null && assertStmt.Proof != null) { |
| | | 4023 | | // clear the labels for the duration of checking the proof body, because break statements are not allowed to l |
| | 0 | 4024 | | var prevLblStmts = enclosingStatementLabels; |
| | 0 | 4025 | | var prevLoopStack = loopStack; |
| | 0 | 4026 | | enclosingStatementLabels = new Scope<Statement>(Options); |
| | 0 | 4027 | | loopStack = new List<Statement>(); |
| | 0 | 4028 | | ResolveStatement(assertStmt.Proof, resolutionContext); |
| | 0 | 4029 | | enclosingStatementLabels = prevLblStmts; |
| | 0 | 4030 | | loopStack = prevLoopStack; |
| | 0 | 4031 | | } |
| | 0 | 4032 | | var expectStmt = stmt as ExpectStmt; |
| | 0 | 4033 | | if (expectStmt != null) { |
| | 0 | 4034 | | if (expectStmt.Message == null) { |
| | 0 | 4035 | | expectStmt.Message = new StringLiteralExpr(s.Tok, "expectation violation", false); |
| | 0 | 4036 | | } |
| | 0 | 4037 | | ResolveExpression(expectStmt.Message, resolutionContext); |
| | 0 | 4038 | | Contract.Assert(expectStmt.Message.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 4039 | | } |
| | | 4040 | | |
| | 760530 | 4041 | | } else if (stmt is PrintStmt) { |
| | 35830 | 4042 | | var s = (PrintStmt)stmt; |
| | 125640 | 4043 | | s.Args.Iter(e => ResolveExpression(e, resolutionContext)); |
| | | 4044 | | |
| | 724700 | 4045 | | } else if (stmt is RevealStmt) { |
| | 0 | 4046 | | var s = (RevealStmt)stmt; |
| | 0 | 4047 | | foreach (var expr in s.Exprs) { |
| | 0 | 4048 | | var name = RevealStmt.SingleName(expr); |
| | 0 | 4049 | | var labeledAssert = name == null ? null : DominatingStatementLabels.Find(name) as AssertLabel; |
| | 0 | 4050 | | if (labeledAssert != null) { |
| | 0 | 4051 | | s.LabeledAsserts.Add(labeledAssert); |
| | 0 | 4052 | | } else { |
| | 0 | 4053 | | var revealResolutionContext = resolutionContext with { InReveal = true }; |
| | 0 | 4054 | | if (expr is ApplySuffix) { |
| | 0 | 4055 | | var e = (ApplySuffix)expr; |
| | 0 | 4056 | | var methodCallInfo = ResolveApplySuffix(e, revealResolutionContext, true); |
| | 0 | 4057 | | if (methodCallInfo == null) { |
| | 0 | 4058 | | reporter.Error(MessageSource.Resolver, expr.tok, "expression has no reveal lemma"); |
| | 0 | 4059 | | } else if (methodCallInfo.Callee.Member is TwoStateLemma && !revealResolutionContext.IsTwoState) { |
| | 0 | 4060 | | reporter.Error(MessageSource.Resolver, methodCallInfo.Tok, "a two-state function can only be revealed in |
| | 0 | 4061 | | } else if (methodCallInfo.Callee.AtLabel != null) { |
| | 0 | 4062 | | Contract.Assert(methodCallInfo.Callee.Member is TwoStateLemma); |
| | 0 | 4063 | | reporter.Error(MessageSource.Resolver, methodCallInfo.Tok, "to reveal a two-state function, do not list |
| | 0 | 4064 | | } else { |
| | 0 | 4065 | | var call = new CallStmt(s.RangeToken, new List<Expression>(), methodCallInfo.Callee, methodCallInfo.Actu |
| | 0 | 4066 | | s.ResolvedStatements.Add(call); |
| | 0 | 4067 | | } |
| | 0 | 4068 | | } else if (expr is NameSegment or ExprDotName) { |
| | 0 | 4069 | | if (expr is NameSegment) { |
| | 0 | 4070 | | ResolveNameSegment((NameSegment)expr, true, null, revealResolutionContext, true); |
| | 0 | 4071 | | } else { |
| | 0 | 4072 | | ResolveDotSuffix((ExprDotName)expr, true, null, revealResolutionContext, true); |
| | 0 | 4073 | | } |
| | 0 | 4074 | | MemberSelectExpr callee = (MemberSelectExpr)((ConcreteSyntaxExpression)expr).ResolvedExpression; |
| | 0 | 4075 | | if (callee == null) { |
| | 0 | 4076 | | } else if (callee.Member is Lemma or TwoStateLemma && Attributes.Contains(callee.Member.Attributes, "axiom |
| | | 4077 | | //The revealed member is a function |
| | 0 | 4078 | | reporter.Error(MessageSource.Resolver, callee.tok, "to reveal a function ({0}), append parentheses", cal |
| | 0 | 4079 | | } else { |
| | 0 | 4080 | | var call = new CallStmt(s.RangeToken, new List<Expression>(), callee, new List<ActualBinding>(), expr.to |
| | 0 | 4081 | | s.ResolvedStatements.Add(call); |
| | 0 | 4082 | | } |
| | 0 | 4083 | | } else { |
| | 0 | 4084 | | ResolveExpression(expr, revealResolutionContext); |
| | 0 | 4085 | | } |
| | 0 | 4086 | | } |
| | 0 | 4087 | | } |
| | 0 | 4088 | | foreach (var a in s.ResolvedStatements) { |
| | 0 | 4089 | | ResolveStatement(a, resolutionContext); |
| | 0 | 4090 | | } |
| | 688870 | 4091 | | } else if (stmt is BreakStmt) { |
| | 0 | 4092 | | var s = (BreakStmt)stmt; |
| | 0 | 4093 | | if (s.TargetLabel != null) { |
| | 0 | 4094 | | Statement target = enclosingStatementLabels.Find(s.TargetLabel.val); |
| | 0 | 4095 | | if (target == null) { |
| | 0 | 4096 | | reporter.Error(MessageSource.Resolver, s.TargetLabel, $"{s.Kind} label is undefined or not in scope: {s.Targ |
| | 0 | 4097 | | } else if (s.IsContinue && !(target is LoopStmt)) { |
| | 0 | 4098 | | reporter.Error(MessageSource.Resolver, s.TargetLabel, $"continue label must designate a loop: {s.TargetLabel |
| | 0 | 4099 | | } else { |
| | 0 | 4100 | | s.TargetStmt = target; |
| | 0 | 4101 | | } |
| | 0 | 4102 | | } else { |
| | 0 | 4103 | | Contract.Assert(1 <= s.BreakAndContinueCount); // follows from BreakStmt class invariant and the guard for thi |
| | 0 | 4104 | | var jumpStmt = s.BreakAndContinueCount == 1 ? |
| | 0 | 4105 | | $"a non-labeled '{s.Kind}' statement" : |
| | 0 | 4106 | | $"a '{Util.Repeat(s.BreakAndContinueCount - 1, "break ")}{s.Kind}' statement"; |
| | 0 | 4107 | | if (loopStack.Count == 0) { |
| | 0 | 4108 | | reporter.Error(MessageSource.Resolver, s, $"{jumpStmt} is allowed only in loops"); |
| | 0 | 4109 | | } else if (loopStack.Count < s.BreakAndContinueCount) { |
| | 0 | 4110 | | reporter.Error(MessageSource.Resolver, s, |
| | 0 | 4111 | | $"{jumpStmt} is allowed only in contexts with {s.BreakAndContinueCount} enclosing loops, but the current c |
| | 0 | 4112 | | } else { |
| | 0 | 4113 | | Statement target = loopStack[loopStack.Count - s.BreakAndContinueCount]; |
| | 0 | 4114 | | if (target.Labels == null) { |
| | | 4115 | | // make sure there is a label, because the compiler and translator will want to see a unique ID |
| | 0 | 4116 | | target.Labels = new LList<Label>(new Label(target.Tok, null), null); |
| | 0 | 4117 | | } |
| | 0 | 4118 | | s.TargetStmt = target; |
| | 0 | 4119 | | } |
| | 0 | 4120 | | } |
| | | 4121 | | |
| | 688870 | 4122 | | } else if (stmt is ProduceStmt) { |
| | 0 | 4123 | | var kind = stmt is YieldStmt ? "yield" : "return"; |
| | 0 | 4124 | | if (stmt is YieldStmt && !(resolutionContext.CodeContext is IteratorDecl)) { |
| | 0 | 4125 | | reporter.Error(MessageSource.Resolver, stmt, "yield statement is allowed only in iterators"); |
| | 0 | 4126 | | } else if (stmt is ReturnStmt && !(resolutionContext.CodeContext is Method)) { |
| | 0 | 4127 | | reporter.Error(MessageSource.Resolver, stmt, "return statement is allowed only in method"); |
| | 0 | 4128 | | } else if (resolutionContext.InFirstPhaseConstructor) { |
| | 0 | 4129 | | reporter.Error(MessageSource.Resolver, stmt, "return statement is not allowed before 'new;' in a constructor") |
| | 0 | 4130 | | } |
| | 0 | 4131 | | var s = (ProduceStmt)stmt; |
| | 0 | 4132 | | if (s.Rhss != null) { |
| | 0 | 4133 | | var cmc = resolutionContext.CodeContext as IMethodCodeContext; |
| | 0 | 4134 | | if (cmc == null) { |
| | | 4135 | | // an error has already been reported above |
| | 0 | 4136 | | } else if (cmc.Outs.Count != s.Rhss.Count) { |
| | 0 | 4137 | | reporter.Error(MessageSource.Resolver, s, "number of {2} parameters does not match declaration (found {0}, e |
| | 0 | 4138 | | } else { |
| | 0 | 4139 | | Contract.Assert(s.Rhss.Count > 0); |
| | | 4140 | | // Create a hidden update statement using the out-parameter formals, resolve the RHS, and check that the RHS |
| | 0 | 4141 | | List<Expression> formals = new List<Expression>(); |
| | 0 | 4142 | | foreach (Formal f in cmc.Outs) { |
| | | 4143 | | Expression produceLhs; |
| | 0 | 4144 | | if (stmt is ReturnStmt) { |
| | 0 | 4145 | | var ident = new ImplicitIdentifierExpr(f.tok, f.Name); |
| | | 4146 | | // resolve it here to avoid capture into more closely declared local variables |
| | 0 | 4147 | | ident.Var = f; |
| | 0 | 4148 | | ident.Type = ident.Var.Type; |
| | 0 | 4149 | | Contract.Assert(f.Type != null); |
| | 0 | 4150 | | produceLhs = ident; |
| | 0 | 4151 | | } else { |
| | 0 | 4152 | | var yieldIdent = new MemberSelectExpr(f.tok, new ImplicitThisExpr(f.tok), f.Name); |
| | 0 | 4153 | | ResolveExpression(yieldIdent, resolutionContext); |
| | 0 | 4154 | | produceLhs = yieldIdent; |
| | 0 | 4155 | | } |
| | 0 | 4156 | | formals.Add(produceLhs); |
| | 0 | 4157 | | } |
| | 0 | 4158 | | s.HiddenUpdate = new UpdateStmt(s.RangeToken, formals, s.Rhss, true); |
| | | 4159 | | // resolving the update statement will check for return/yield statement specifics. |
| | 0 | 4160 | | ResolveStatement(s.HiddenUpdate, resolutionContext); |
| | 0 | 4161 | | } |
| | 0 | 4162 | | } else {// this is a regular return/yield statement. |
| | 0 | 4163 | | s.HiddenUpdate = null; |
| | 0 | 4164 | | } |
| | 998320 | 4165 | | } else if (stmt is VarDeclStmt) { |
| | 309450 | 4166 | | var s = (VarDeclStmt)stmt; |
| | | 4167 | | // We have four cases. |
| | 309450 | 4168 | | Contract.Assert(s.Update == null || s.Update is AssignSuchThatStmt || s.Update is UpdateStmt || s.Update is Assi |
| | | 4169 | | // 0. There is no .Update. This is easy, we will just resolve the locals. |
| | | 4170 | | // 1. The .Update is an AssignSuchThatStmt. This is also straightforward: first |
| | | 4171 | | // resolve the locals, which adds them to the scope, and then resolve the .Update. |
| | | 4172 | | // 2. The .Update is an UpdateStmt, which, resolved, means either a CallStmt or a bunch |
| | | 4173 | | // of parallel AssignStmt's. Here, the right-hand sides should be resolved before |
| | | 4174 | | // the local variables have been added to the scope, but the left-hand sides should |
| | | 4175 | | // resolve to the newly introduced variables. |
| | | 4176 | | // 3. The .Update is a ":-" statement, for which resolution does two steps: |
| | | 4177 | | // First, desugar, then run the regular resolution on the desugared AST. |
| | | 4178 | | // To accommodate these options, we first reach into the UpdateStmt, if any, to resolve |
| | | 4179 | | // the left-hand sides of the UpdateStmt. This will have the effect of shielding them |
| | | 4180 | | // from a subsequent resolution (since expression resolution will do nothing if the .Type |
| | | 4181 | | // field is already assigned. |
| | | 4182 | | // Alright, so it is: |
| | | 4183 | | |
| | | 4184 | | // Resolve the types of the locals |
| | 1871430 | 4185 | | foreach (var local in s.Locals) { |
| | 314360 | 4186 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 314360 | 4187 | | ResolveType(local.Tok, local.OptionalType, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 628720 | 4188 | | if (reporter.Count(ErrorLevel.Error) == prevErrorCount) { |
| | 314360 | 4189 | | local.type = local.OptionalType; |
| | 314360 | 4190 | | } else { |
| | 0 | 4191 | | local.type = new InferredTypeProxy(); |
| | 0 | 4192 | | } |
| | 314360 | 4193 | | } |
| | | 4194 | | // Resolve the UpdateStmt, if any |
| | 618900 | 4195 | | if (s.Update is UpdateStmt) { |
| | 309450 | 4196 | | var upd = (UpdateStmt)s.Update; |
| | | 4197 | | // resolve the LHS |
| | 309450 | 4198 | | Contract.Assert(upd.Lhss.Count == s.Locals.Count); |
| | 1561980 | 4199 | | for (int i = 0; i < upd.Lhss.Count; i++) { |
| | 314360 | 4200 | | var local = s.Locals[i]; |
| | 314360 | 4201 | | var lhs = (IdentifierExpr)upd.Lhss[i]; // the LHS in this case will be an IdentifierExpr, because that's ho |
| | 314360 | 4202 | | Contract.Assert(lhs.Type == null); // not yet resolved |
| | 314360 | 4203 | | lhs.Var = local; |
| | 314360 | 4204 | | lhs.Type = local.Type; |
| | 314360 | 4205 | | } |
| | | 4206 | | // resolve the whole thing |
| | 309450 | 4207 | | s.Update.Resolve(this, resolutionContext); |
| | 309450 | 4208 | | } |
| | | 4209 | | |
| | 309450 | 4210 | | if (s.Update is AssignOrReturnStmt) { |
| | 0 | 4211 | | var assignOrRet = (AssignOrReturnStmt)s.Update; |
| | | 4212 | | // resolve the LHS |
| | 0 | 4213 | | Contract.Assert(assignOrRet.Lhss.Count == s.Locals.Count); |
| | 0 | 4214 | | for (int i = 0; i < s.Locals.Count; i++) { |
| | 0 | 4215 | | var local = s.Locals[i]; |
| | 0 | 4216 | | var lhs = (IdentifierExpr)assignOrRet |
| | 0 | 4217 | | .Lhss[i]; // the LHS in this case will be an IdentifierExpr, because that's how the parser creates the Var |
| | 0 | 4218 | | Contract.Assert(lhs.Type == null); // not yet resolved |
| | 0 | 4219 | | lhs.Var = local; |
| | 0 | 4220 | | lhs.Type = local.Type; |
| | 0 | 4221 | | } |
| | | 4222 | | |
| | | 4223 | | // resolve the whole thing |
| | 0 | 4224 | | assignOrRet.Resolve(this, resolutionContext); |
| | 0 | 4225 | | } |
| | | 4226 | | // Add the locals to the scope |
| | 1871430 | 4227 | | foreach (var local in s.Locals) { |
| | 314360 | 4228 | | ScopePushAndReport(scope, local, "local-variable"); |
| | 314360 | 4229 | | } |
| | | 4230 | | // With the new locals in scope, it's now time to resolve the attributes on all the locals |
| | 1871430 | 4231 | | foreach (var local in s.Locals) { |
| | 314360 | 4232 | | ResolveAttributes(local, resolutionContext); |
| | 314360 | 4233 | | } |
| | | 4234 | | // Resolve the AssignSuchThatStmt, if any |
| | 309450 | 4235 | | if (s.Update is AssignSuchThatStmt assignSuchThatStmt) { |
| | 0 | 4236 | | assignSuchThatStmt.Resolve(this, resolutionContext); |
| | 0 | 4237 | | } |
| | 688870 | 4238 | | } else if (stmt is VarDeclPattern) { |
| | 0 | 4239 | | VarDeclPattern s = (VarDeclPattern)stmt; |
| | 0 | 4240 | | foreach (var local in s.LocalVars) { |
| | 0 | 4241 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 4242 | | ResolveType(local.Tok, local.OptionalType, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 4243 | | if (reporter.Count(ErrorLevel.Error) == prevErrorCount) { |
| | 0 | 4244 | | local.type = local.OptionalType; |
| | 0 | 4245 | | } else { |
| | 0 | 4246 | | local.type = new InferredTypeProxy(); |
| | 0 | 4247 | | } |
| | 0 | 4248 | | } |
| | 0 | 4249 | | ResolveExpression(s.RHS, resolutionContext); |
| | 0 | 4250 | | ResolveCasePattern(s.LHS, s.RHS.Type, resolutionContext); |
| | | 4251 | | // Check for duplicate names now, because not until after resolving the case pattern do we know if identifiers i |
| | 0 | 4252 | | var c = 0; |
| | 0 | 4253 | | foreach (var bv in s.LHS.Vars) { |
| | 0 | 4254 | | ScopePushAndReport(scope, bv, "local_variable"); |
| | 0 | 4255 | | c++; |
| | 0 | 4256 | | } |
| | 0 | 4257 | | if (c == 0) { |
| | | 4258 | | // Every identifier-looking thing in the pattern resolved to a constructor; that is, this LHS is a constant li |
| | 0 | 4259 | | reporter.Error(MessageSource.Resolver, s.LHS.tok, "LHS is a constant literal; to be legal, it must introduce a |
| | 0 | 4260 | | } |
| | 718990 | 4261 | | } else if (stmt is AssignStmt) { |
| | 339570 | 4262 | | AssignStmt s = (AssignStmt)stmt; |
| | 339570 | 4263 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 339570 | 4264 | | ResolveExpression(s.Lhs, resolutionContext); // allow ghosts for now, tighted up below |
| | 339570 | 4265 | | bool lhsResolvedSuccessfully = reporter.Count(ErrorLevel.Error) == prevErrorCount; |
| | 339570 | 4266 | | Contract.Assert(s.Lhs.Type != null); // follows from postcondition of ResolveExpression |
| | | 4267 | | // check that LHS denotes a mutable variable or a field |
| | 339570 | 4268 | | var lhs = s.Lhs.Resolved; |
| | 679140 | 4269 | | if (lhs is IdentifierExpr) { |
| | 339570 | 4270 | | IVariable var = ((IdentifierExpr)lhs).Var; |
| | 339570 | 4271 | | if (var == null) { |
| | | 4272 | | // the LHS didn't resolve correctly; some error would already have been reported |
| | 339570 | 4273 | | } else { |
| | 339570 | 4274 | | CheckIsLvalue(lhs, resolutionContext); |
| | 339570 | 4275 | | } |
| | 339570 | 4276 | | } else if (lhs is MemberSelectExpr) { |
| | 0 | 4277 | | var fse = (MemberSelectExpr)lhs; |
| | 0 | 4278 | | if (fse.Member != null) { // otherwise, an error was reported above |
| | 0 | 4279 | | CheckIsLvalue(fse, resolutionContext); |
| | 0 | 4280 | | } |
| | 0 | 4281 | | } else if (lhs is SeqSelectExpr) { |
| | 0 | 4282 | | var slhs = (SeqSelectExpr)lhs; |
| | | 4283 | | // LHS is fine, provided the "sequence" is really an array |
| | 0 | 4284 | | if (lhsResolvedSuccessfully) { |
| | 0 | 4285 | | Contract.Assert(slhs.Seq.Type != null); |
| | 0 | 4286 | | CheckIsLvalue(slhs, resolutionContext); |
| | 0 | 4287 | | } |
| | 0 | 4288 | | } else if (lhs is MultiSelectExpr) { |
| | 0 | 4289 | | CheckIsLvalue(lhs, resolutionContext); |
| | 0 | 4290 | | } else { |
| | 0 | 4291 | | CheckIsLvalue(lhs, resolutionContext); |
| | 0 | 4292 | | } |
| | 339570 | 4293 | | Type lhsType = s.Lhs.Type; |
| | 679140 | 4294 | | if (s.Rhs is ExprRhs) { |
| | 339570 | 4295 | | ExprRhs rr = (ExprRhs)s.Rhs; |
| | 339570 | 4296 | | ResolveExpression(rr.Expr, resolutionContext); |
| | 339570 | 4297 | | Contract.Assert(rr.Expr.Type != null); // follows from postcondition of ResolveExpression |
| | | 4298 | | |
| | 339570 | 4299 | | if (s.Lhs is ImplicitIdentifierExpr { Var: Formal { InParam: false } }) { |
| | 0 | 4300 | | AddAssignableConstraint(stmt.Tok, lhsType, rr.Expr.Type, "Method return value mismatch (expected {0}, got {1 |
| | 339570 | 4301 | | } else { |
| | 339570 | 4302 | | AddAssignableConstraint(stmt.Tok, lhsType, rr.Expr.Type, "RHS (of type {1}) not assignable to LHS (of type { |
| | 339570 | 4303 | | } |
| | 339570 | 4304 | | } else if (s.Rhs is TypeRhs) { |
| | 0 | 4305 | | TypeRhs rr = (TypeRhs)s.Rhs; |
| | 0 | 4306 | | Type t = ResolveTypeRhs(rr, stmt, resolutionContext); |
| | 0 | 4307 | | AddAssignableConstraint(stmt.Tok, lhsType, t, "type {1} is not assignable to LHS (of type {0})"); |
| | 0 | 4308 | | } else if (s.Rhs is HavocRhs) { |
| | | 4309 | | // nothing else to do |
| | 0 | 4310 | | } else { |
| | 0 | 4311 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected RHS |
| | | 4312 | | } |
| | | 4313 | | |
| | 389540 | 4314 | | } else if (stmt is CallStmt) { |
| | 10120 | 4315 | | CallStmt s = (CallStmt)stmt; |
| | 10120 | 4316 | | ResolveCallStmt(s, resolutionContext, null); |
| | | 4317 | | |
| | 64160 | 4318 | | } else if (stmt is BlockStmt) { |
| | 24310 | 4319 | | var s = (BlockStmt)stmt; |
| | 24310 | 4320 | | scope.PushMarker(); |
| | 24310 | 4321 | | ResolveBlockStatement(s, resolutionContext); |
| | 24310 | 4322 | | scope.PopMarker(); |
| | | 4323 | | |
| | 35150 | 4324 | | } else if (stmt is IfStmt) { |
| | 5420 | 4325 | | IfStmt s = (IfStmt)stmt; |
| | 10840 | 4326 | | if (s.Guard != null) { |
| | 5420 | 4327 | | ResolveExpression(s.Guard, resolutionContext); |
| | 5420 | 4328 | | Contract.Assert(s.Guard.Type != null); // follows from postcondition of ResolveExpression |
| | 5420 | 4329 | | ConstrainTypeExprBool(s.Guard, "condition is expected to be of type bool, but is {0}"); |
| | 5420 | 4330 | | } |
| | | 4331 | | |
| | 5420 | 4332 | | scope.PushMarker(); |
| | 5420 | 4333 | | if (s.IsBindingGuard) { |
| | 0 | 4334 | | var exists = (ExistsExpr)s.Guard; |
| | 0 | 4335 | | foreach (var v in exists.BoundVars) { |
| | 0 | 4336 | | ScopePushAndReport(scope, v, "bound-variable"); |
| | 0 | 4337 | | } |
| | 0 | 4338 | | } |
| | 5420 | 4339 | | DominatingStatementLabels.PushMarker(); |
| | 5420 | 4340 | | ResolveBlockStatement(s.Thn, resolutionContext); |
| | 5420 | 4341 | | DominatingStatementLabels.PopMarker(); |
| | 5420 | 4342 | | scope.PopMarker(); |
| | | 4343 | | |
| | 10840 | 4344 | | if (s.Els != null) { |
| | 5420 | 4345 | | DominatingStatementLabels.PushMarker(); |
| | 5420 | 4346 | | ResolveStatement(s.Els, resolutionContext); |
| | 5420 | 4347 | | DominatingStatementLabels.PopMarker(); |
| | 5420 | 4348 | | } |
| | | 4349 | | |
| | 5420 | 4350 | | } else if (stmt is AlternativeStmt) { |
| | 0 | 4351 | | var s = (AlternativeStmt)stmt; |
| | 0 | 4352 | | ResolveAlternatives(s.Alternatives, null, resolutionContext); |
| | | 4353 | | |
| | 0 | 4354 | | } else if (stmt is OneBodyLoopStmt) { |
| | 0 | 4355 | | var s = (OneBodyLoopStmt)stmt; |
| | 0 | 4356 | | if (s is WhileStmt whileS && whileS.Guard != null) { |
| | 0 | 4357 | | ResolveExpression(whileS.Guard, resolutionContext); |
| | 0 | 4358 | | Contract.Assert(whileS.Guard.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 4359 | | ConstrainTypeExprBool(whileS.Guard, "condition is expected to be of type bool, but is {0}"); |
| | 0 | 4360 | | } else if (s is ForLoopStmt forS) { |
| | 0 | 4361 | | var loopIndex = forS.LoopIndex; |
| | 0 | 4362 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 4363 | | ResolveType(loopIndex.Tok, loopIndex.Type, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 4364 | | var err = new TypeConstraint.ErrorMsgWithToken(loopIndex.Tok, "index variable is expected to be of an integer |
| | 0 | 4365 | | ConstrainToIntegerType(loopIndex.Tok, loopIndex.Type, false, err); |
| | | 4366 | | |
| | 0 | 4367 | | ResolveExpression(forS.Start, resolutionContext); |
| | 0 | 4368 | | AddAssignableConstraint(forS.Start.tok, forS.LoopIndex.Type, forS.Start.Type, "lower bound (of type {1}) not a |
| | 0 | 4369 | | if (forS.End != null) { |
| | 0 | 4370 | | ResolveExpression(forS.End, resolutionContext); |
| | 0 | 4371 | | AddAssignableConstraint(forS.End.tok, forS.LoopIndex.Type, forS.End.Type, "upper bound (of type {1}) not ass |
| | 0 | 4372 | | if (forS.Decreases.Expressions.Count != 0) { |
| | 0 | 4373 | | reporter.Error(MessageSource.Resolver, forS.Decreases.Expressions[0].tok, |
| | 0 | 4374 | | "a 'for' loop is allowed an explicit 'decreases' clause only if the end-expression is '*'"); |
| | 0 | 4375 | | } |
| | 0 | 4376 | | } else if (forS.Decreases.Expressions.Count == 0 && !resolutionContext.CodeContext.AllowsNontermination) { |
| | | 4377 | | // note, the following error message is also emitted elsewhere (if the loop bears a "decreases *") |
| | 0 | 4378 | | reporter.Error(MessageSource.Resolver, forS.Tok, |
| | 0 | 4379 | | "a possibly infinite loop is allowed only if the enclosing method is declared (with 'decreases *') to be p |
| | 0 | 4380 | | " (or you can add a 'decreases' clause to this 'for' loop if you want to prove that it does indeed termina |
| | 0 | 4381 | | } |
| | | 4382 | | |
| | | 4383 | | // Create a new scope, add the local to the scope, and resolve the attributes |
| | 0 | 4384 | | scope.PushMarker(); |
| | 0 | 4385 | | ScopePushAndReport(scope, loopIndex, "index-variable"); |
| | 0 | 4386 | | ResolveAttributes(s, resolutionContext); |
| | 0 | 4387 | | } |
| | | 4388 | | |
| | 0 | 4389 | | ResolveLoopSpecificationComponents(s.Invariants, s.Decreases, s.Mod, resolutionContext); |
| | | 4390 | | |
| | 0 | 4391 | | if (s.Body != null) { |
| | 0 | 4392 | | loopStack.Add(s); // push |
| | 0 | 4393 | | DominatingStatementLabels.PushMarker(); |
| | 0 | 4394 | | ResolveStatement(s.Body, resolutionContext); |
| | 0 | 4395 | | DominatingStatementLabels.PopMarker(); |
| | 0 | 4396 | | loopStack.RemoveAt(loopStack.Count - 1); // pop |
| | 0 | 4397 | | } |
| | | 4398 | | |
| | 0 | 4399 | | if (s is ForLoopStmt) { |
| | 0 | 4400 | | scope.PopMarker(); |
| | 0 | 4401 | | } |
| | | 4402 | | |
| | 0 | 4403 | | } else if (stmt is AlternativeLoopStmt) { |
| | 0 | 4404 | | var s = (AlternativeLoopStmt)stmt; |
| | 0 | 4405 | | ResolveAlternatives(s.Alternatives, s, resolutionContext); |
| | 0 | 4406 | | ResolveLoopSpecificationComponents(s.Invariants, s.Decreases, s.Mod, resolutionContext); |
| | | 4407 | | |
| | 0 | 4408 | | } else if (stmt is ForallStmt) { |
| | 0 | 4409 | | var s = (ForallStmt)stmt; |
| | | 4410 | | |
| | 0 | 4411 | | int prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 4412 | | scope.PushMarker(); |
| | 0 | 4413 | | foreach (BoundVar v in s.BoundVars) { |
| | 0 | 4414 | | ScopePushAndReport(scope, v, "local-variable"); |
| | 0 | 4415 | | ResolveType(v.tok, v.Type, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 4416 | | } |
| | 0 | 4417 | | ResolveExpression(s.Range, resolutionContext); |
| | 0 | 4418 | | Contract.Assert(s.Range.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 4419 | | ConstrainTypeExprBool(s.Range, "range restriction in forall statement must be of type bool (instead got {0})"); |
| | 0 | 4420 | | foreach (var ens in s.Ens) { |
| | 0 | 4421 | | ResolveExpression(ens.E, resolutionContext); |
| | 0 | 4422 | | Contract.Assert(ens.E.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 4423 | | ConstrainTypeExprBool(ens.E, "ensures condition is expected to be of type bool, but is {0}"); |
| | 0 | 4424 | | } |
| | | 4425 | | // Since the range and postconditions are more likely to infer the types of the bound variables, resolve them |
| | | 4426 | | // first (above) and only then resolve the attributes (below). |
| | 0 | 4427 | | ResolveAttributes(s, resolutionContext); |
| | | 4428 | | |
| | 0 | 4429 | | if (s.Body != null) { |
| | | 4430 | | // clear the labels for the duration of checking the body, because break statements are not allowed to leave a |
| | 0 | 4431 | | var prevLblStmts = enclosingStatementLabels; |
| | 0 | 4432 | | var prevLoopStack = loopStack; |
| | 0 | 4433 | | enclosingStatementLabels = new Scope<Statement>(Options); |
| | 0 | 4434 | | loopStack = new List<Statement>(); |
| | 0 | 4435 | | ResolveStatement(s.Body, resolutionContext); |
| | 0 | 4436 | | enclosingStatementLabels = prevLblStmts; |
| | 0 | 4437 | | loopStack = prevLoopStack; |
| | 0 | 4438 | | } |
| | 0 | 4439 | | scope.PopMarker(); |
| | | 4440 | | |
| | 0 | 4441 | | if (prevErrorCount == reporter.Count(ErrorLevel.Error)) { |
| | | 4442 | | // determine the Kind and run some additional checks on the body |
| | 0 | 4443 | | if (s.Ens.Count != 0) { |
| | | 4444 | | // The only supported kind with ensures clauses is Proof. |
| | 0 | 4445 | | s.Kind = ForallStmt.BodyKind.Proof; |
| | 0 | 4446 | | } else { |
| | | 4447 | | // There are three special cases: |
| | | 4448 | | // * Assign, which is the only kind of the forall statement that allows a heap update. |
| | | 4449 | | // * Call, which is a single call statement with no side effects or output parameters. |
| | | 4450 | | // * A single calc statement, which is a special case of Proof where the postcondition can be inferred. |
| | | 4451 | | // The effect of Assign and the postcondition of Call will be seen outside the forall |
| | | 4452 | | // statement. |
| | 0 | 4453 | | Statement s0 = s.S0; |
| | 0 | 4454 | | if (s0 is AssignStmt) { |
| | 0 | 4455 | | s.Kind = ForallStmt.BodyKind.Assign; |
| | | 4456 | | |
| | 0 | 4457 | | var rhs = ((AssignStmt)s0).Rhs; |
| | 0 | 4458 | | if (rhs is TypeRhs) { |
| | 0 | 4459 | | reporter.Error(MessageSource.Resolver, rhs.Tok, "new allocation not supported in aggregate assignments") |
| | 0 | 4460 | | } |
| | | 4461 | | |
| | 0 | 4462 | | } else if (s0 is CallStmt) { |
| | 0 | 4463 | | s.Kind = ForallStmt.BodyKind.Call; |
| | 0 | 4464 | | var call = (CallStmt)s.S0; |
| | 0 | 4465 | | var method = call.Method; |
| | | 4466 | | // if the called method is not in the same module as the ForallCall stmt |
| | | 4467 | | // don't convert it to ForallExpression since the inlined called method's |
| | | 4468 | | // ensure clause might not be resolved correctly(test\dafny3\GenericSort.dfy) |
| | 0 | 4469 | | if (method.EnclosingClass.EnclosingModuleDefinition != resolutionContext.CodeContext.EnclosingModule) { |
| | 0 | 4470 | | s.CanConvert = false; |
| | 0 | 4471 | | } |
| | | 4472 | | // Additional information (namely, the postcondition of the call) will be reported later. But it cannot be |
| | | 4473 | | // done yet, because the specification of the callee may not have been resolved yet. |
| | 0 | 4474 | | } else if (s0 is CalcStmt) { |
| | 0 | 4475 | | s.Kind = ForallStmt.BodyKind.Proof; |
| | | 4476 | | // add the conclusion of the calc as a free postcondition |
| | 0 | 4477 | | var result = ((CalcStmt)s0).Result; |
| | 0 | 4478 | | s.Ens.Add(new AttributedExpression(result)); |
| | 0 | 4479 | | reporter.Info(MessageSource.Resolver, s.Tok, "ensures " + Printer.ExprToString(Options, result)); |
| | 0 | 4480 | | } else { |
| | 0 | 4481 | | s.Kind = ForallStmt.BodyKind.Proof; |
| | 0 | 4482 | | if (s.Body is BlockStmt && ((BlockStmt)s.Body).Body.Count == 0) { |
| | | 4483 | | // an empty statement, so don't produce any warning |
| | 0 | 4484 | | } else { |
| | 0 | 4485 | | reporter.Warning(MessageSource.Resolver, ErrorRegistry.NoneId, s.Tok, "the conclusion of the body of thi |
| | 0 | 4486 | | } |
| | 0 | 4487 | | } |
| | 0 | 4488 | | } |
| | | 4489 | | |
| | 0 | 4490 | | if (s.ForallExpressions != null) { |
| | 0 | 4491 | | foreach (Expression expr in s.ForallExpressions) { |
| | 0 | 4492 | | ResolveExpression(expr, resolutionContext); |
| | 0 | 4493 | | } |
| | 0 | 4494 | | } |
| | 0 | 4495 | | } |
| | | 4496 | | |
| | 0 | 4497 | | } else if (stmt is ModifyStmt) { |
| | 0 | 4498 | | var s = (ModifyStmt)stmt; |
| | 0 | 4499 | | ResolveAttributes(s.Mod, resolutionContext); |
| | 0 | 4500 | | foreach (FrameExpression fe in s.Mod.Expressions) { |
| | 0 | 4501 | | ResolveFrameExpression(fe, FrameExpressionUse.Modifies, resolutionContext); |
| | 0 | 4502 | | } |
| | 0 | 4503 | | if (s.Body != null) { |
| | 0 | 4504 | | ResolveBlockStatement(s.Body, resolutionContext); |
| | 0 | 4505 | | } |
| | | 4506 | | |
| | 0 | 4507 | | } else if (stmt is CalcStmt) { |
| | 0 | 4508 | | var prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 0 | 4509 | | CalcStmt s = (CalcStmt)stmt; |
| | | 4510 | | // figure out s.Op |
| | 0 | 4511 | | Contract.Assert(s.Op == null); // it hasn't been set yet |
| | 0 | 4512 | | if (s.UserSuppliedOp != null) { |
| | 0 | 4513 | | s.Op = s.UserSuppliedOp; |
| | 0 | 4514 | | } else { |
| | 0 | 4515 | | s.Op = s.GetInferredDefaultOp() ?? CalcStmt.DefaultOp; |
| | 0 | 4516 | | reporter.Info(MessageSource.Resolver, s.Tok, s.Op.ToString()); |
| | 0 | 4517 | | } |
| | | 4518 | | |
| | 0 | 4519 | | if (s.Lines.Count > 0) { |
| | 0 | 4520 | | Type lineType = new InferredTypeProxy(); |
| | 0 | 4521 | | var e0 = s.Lines.First(); |
| | 0 | 4522 | | ResolveExpression(e0, resolutionContext); |
| | 0 | 4523 | | Contract.Assert(e0.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 4524 | | var err = new TypeConstraint.ErrorMsgWithToken(e0.tok, "all lines in a calculation must have the same type (go |
| | 0 | 4525 | | ConstrainSubtypeRelation(lineType, e0.Type, err); |
| | 0 | 4526 | | for (int i = 1; i < s.Lines.Count; i++) { |
| | 0 | 4527 | | var e1 = s.Lines[i]; |
| | 0 | 4528 | | ResolveExpression(e1, resolutionContext); |
| | 0 | 4529 | | Contract.Assert(e1.Type != null); // follows from postcondition of ResolveExpression |
| | | 4530 | | // reuse the error object if we're on the dummy line; this prevents a duplicate error message |
| | 0 | 4531 | | if (i < s.Lines.Count - 1) { |
| | 0 | 4532 | | err = new TypeConstraint.ErrorMsgWithToken(e1.tok, "all lines in a calculation must have the same type (go |
| | 0 | 4533 | | } |
| | 0 | 4534 | | ConstrainSubtypeRelation(lineType, e1.Type, err); |
| | 0 | 4535 | | var step = (s.StepOps[i - 1] ?? s.Op).StepExpr(e0, e1); // Use custom line operator |
| | 0 | 4536 | | ResolveExpression(step, resolutionContext); |
| | 0 | 4537 | | s.Steps.Add(step); |
| | 0 | 4538 | | e0 = e1; |
| | 0 | 4539 | | } |
| | | 4540 | | |
| | | 4541 | | // clear the labels for the duration of checking the hints, because break statements are not allowed to leave |
| | 0 | 4542 | | var prevLblStmts = enclosingStatementLabels; |
| | 0 | 4543 | | var prevLoopStack = loopStack; |
| | 0 | 4544 | | enclosingStatementLabels = new Scope<Statement>(Options); |
| | 0 | 4545 | | loopStack = new List<Statement>(); |
| | 0 | 4546 | | foreach (var h in s.Hints) { |
| | 0 | 4547 | | foreach (var oneHint in h.Body) { |
| | 0 | 4548 | | DominatingStatementLabels.PushMarker(); |
| | 0 | 4549 | | ResolveStatement(oneHint, resolutionContext); |
| | 0 | 4550 | | DominatingStatementLabels.PopMarker(); |
| | 0 | 4551 | | } |
| | 0 | 4552 | | } |
| | 0 | 4553 | | enclosingStatementLabels = prevLblStmts; |
| | 0 | 4554 | | loopStack = prevLoopStack; |
| | | 4555 | | |
| | 0 | 4556 | | } |
| | 0 | 4557 | | if (prevErrorCount == reporter.Count(ErrorLevel.Error) && s.Lines.Count > 0) { |
| | | 4558 | | // do not build Result from the lines if there were errors, as it might be ill-typed and produce unnecessary r |
| | 0 | 4559 | | var resultOp = s.StepOps.Aggregate(s.Op, (op0, op1) => op1 == null ? op0 : op0.ResultOp(op1)); |
| | 0 | 4560 | | s.Result = resultOp.StepExpr(s.Lines.First(), s.Lines.Last()); |
| | 0 | 4561 | | } else { |
| | 0 | 4562 | | s.Result = CalcStmt.DefaultOp.StepExpr(Expression.CreateIntLiteral(s.Tok, 0), Expression.CreateIntLiteral(s.To |
| | 0 | 4563 | | } |
| | 0 | 4564 | | ResolveExpression(s.Result, resolutionContext); |
| | 0 | 4565 | | Contract.Assert(s.Result != null); |
| | 0 | 4566 | | Contract.Assert(prevErrorCount != reporter.Count(ErrorLevel.Error) || s.Steps.Count == s.Hints.Count); |
| | | 4567 | | |
| | 0 | 4568 | | } else if (stmt is SkeletonStatement) { |
| | 0 | 4569 | | var s = (SkeletonStatement)stmt; |
| | 0 | 4570 | | reporter.Error(MessageSource.Resolver, s.Tok, "skeleton statements are allowed only in refining methods"); |
| | | 4571 | | // nevertheless, resolve the underlying statement; hey, why not |
| | 0 | 4572 | | if (s.S != null) { |
| | 0 | 4573 | | ResolveStatement(s.S, resolutionContext); |
| | 0 | 4574 | | } |
| | 0 | 4575 | | } else { |
| | 0 | 4576 | | Contract.Assert(false); throw new cce.UnreachableException(); |
| | | 4577 | | } |
| | 764940 | 4578 | | } |
| | | 4579 | | |
| | | 4580 | | private void ResolveLoopSpecificationComponents(List<AttributedExpression> invariants, Specification<Expression> dec |
| | 0 | 4581 | | Specification<FrameExpression> modifies, ResolutionContext resolutionContext) { |
| | | 4582 | | Contract.Requires(invariants != null); |
| | | 4583 | | Contract.Requires(decreases != null); |
| | | 4584 | | Contract.Requires(modifies != null); |
| | | 4585 | | Contract.Requires(resolutionContext != null); |
| | | 4586 | | |
| | 0 | 4587 | | foreach (AttributedExpression inv in invariants) { |
| | 0 | 4588 | | ResolveAttributes(inv, resolutionContext); |
| | 0 | 4589 | | ResolveExpression(inv.E, resolutionContext); |
| | 0 | 4590 | | Contract.Assert(inv.E.Type != null); // follows from postcondition of ResolveExpression |
| | 0 | 4591 | | ConstrainTypeExprBool(inv.E, "invariant is expected to be of type bool, but is {0}"); |
| | 0 | 4592 | | } |
| | | 4593 | | |
| | 0 | 4594 | | ResolveAttributes(decreases, resolutionContext); |
| | 0 | 4595 | | foreach (Expression e in decreases.Expressions) { |
| | 0 | 4596 | | ResolveExpression(e, resolutionContext); |
| | 0 | 4597 | | if (e is WildcardExpr && !resolutionContext.CodeContext.AllowsNontermination) { |
| | 0 | 4598 | | reporter.Error(MessageSource.Resolver, e, "a possibly infinite loop is allowed only if the enclosing method is |
| | 0 | 4599 | | } |
| | | 4600 | | // any type is fine |
| | 0 | 4601 | | } |
| | | 4602 | | |
| | 0 | 4603 | | ResolveAttributes(modifies, resolutionContext); |
| | 0 | 4604 | | if (modifies.Expressions != null) { |
| | 0 | 4605 | | foreach (FrameExpression fe in modifies.Expressions) { |
| | 0 | 4606 | | ResolveFrameExpression(fe, FrameExpressionUse.Modifies, resolutionContext); |
| | 0 | 4607 | | } |
| | 0 | 4608 | | } |
| | 0 | 4609 | | } |
| | | 4610 | | |
| | | 4611 | | /// <summary> |
| | | 4612 | | /// Resolves the default-valued expression for each formal in "formals". |
| | | 4613 | | /// Solves the resulting type constraints. |
| | | 4614 | | /// Assumes these are the only type constraints to be solved. |
| | | 4615 | | /// |
| | | 4616 | | /// Reports an error for any cyclic dependency among the default-value expressions of the formals. |
| | | 4617 | | /// </summary> |
| | 16690 | 4618 | | public void ResolveParameterDefaultValues(List<Formal> formals, ResolutionContext resolutionContext) { |
| | | 4619 | | Contract.Requires(formals != null); |
| | | 4620 | | Contract.Requires(resolutionContext != null); |
| | | 4621 | | |
| | 16690 | 4622 | | Contract.Assume(AllTypeConstraints.Count == 0); |
| | | 4623 | | |
| | | 4624 | | // Formal parameters have three ways to indicate how they are to be passed in: |
| | | 4625 | | // * nameonly: the only way to give a specific argument value is to name the parameter |
| | | 4626 | | // * positional only: these are nameless parameters (which are allowed only for datatype constructor parameters) |
| | | 4627 | | // * either positional or by name: this is the most common parameter |
| | | 4628 | | // A parameter is either required or optional: |
| | | 4629 | | // * required: a caller has to supply an argument |
| | | 4630 | | // * optional: the parameter has a default value that is used if a caller omits passing a specific argument |
| | | 4631 | | // |
| | | 4632 | | // The syntax for giving a positional-only (i.e., nameless) parameter does not allow a default-value expression, s |
| | | 4633 | | // a positional-only parameter is always required. |
| | | 4634 | | // |
| | | 4635 | | // At a call site, positional arguments are not allowed to follow named arguments. Therefore, if "x" is |
| | | 4636 | | // a nameonly parameter, then there is no way to supply the parameters after "x" by position. Thus, any |
| | | 4637 | | // parameter that follows "x" must either be passed by name or have a default value. That is, if a later |
| | | 4638 | | // parameter does not have a default value, it is _effectively_ nameonly. We impose the rule that |
| | | 4639 | | // * an effectively nameonly parameter must be declared as nameonly |
| | | 4640 | | // |
| | | 4641 | | // For a positional-only parameter "x", every parameter preceding "x" is _effectively_ required. We impose |
| | | 4642 | | // the rule that |
| | | 4643 | | // * an effectively required parameter must not have a default-value expression |
| | 16690 | 4644 | | var dependencies = new Graph<IVariable>(); |
| | 16690 | 4645 | | string nameOfMostRecentNameonlyParameter = null; |
| | 16690 | 4646 | | var previousParametersWithDefaultValue = new HashSet<Formal>(); |
| | 173190 | 4647 | | foreach (var formal in formals) { |
| | 41040 | 4648 | | if (!formal.HasName) { |
| | 0 | 4649 | | foreach (var previousFormal in previousParametersWithDefaultValue) { |
| | 0 | 4650 | | reporter.Error(MessageSource.Resolver, previousFormal.DefaultValue.tok, |
| | 0 | 4651 | | $"because of a later nameless parameter, this default value is never used; remove it or name all subsequen |
| | 0 | 4652 | | } |
| | 0 | 4653 | | previousParametersWithDefaultValue.Clear(); |
| | 0 | 4654 | | } |
| | 41040 | 4655 | | var d = formal.DefaultValue; |
| | 41040 | 4656 | | if (d != null) { |
| | 0 | 4657 | | ResolveExpression(d, resolutionContext); |
| | 0 | 4658 | | AddAssignableConstraint(d.tok, formal.Type, d.Type, "default-value expression (of type '{1}') is not assignabl |
| | 0 | 4659 | | foreach (var v in FreeVariables(d)) { |
| | 0 | 4660 | | dependencies.AddEdge(formal, v); |
| | 0 | 4661 | | } |
| | 0 | 4662 | | previousParametersWithDefaultValue.Add(formal); |
| | 41040 | 4663 | | } else if (nameOfMostRecentNameonlyParameter != null && !formal.IsNameOnly) { |
| | | 4664 | | // "formal" is preceded by a nameonly parameter, but itself is neither nameonly nor has a default value |
| | 0 | 4665 | | reporter.Error(MessageSource.Resolver, formal.tok, |
| | 0 | 4666 | | $"this parameter is effectively nameonly (because of the earlier nameonly parameter '{nameOfMostRecentNameon |
| | 0 | 4667 | | "declare it as nameonly or give it a default-value expression"); |
| | 0 | 4668 | | } |
| | 41040 | 4669 | | if (formal.IsNameOnly) { |
| | 0 | 4670 | | nameOfMostRecentNameonlyParameter = formal.Name; |
| | 0 | 4671 | | } |
| | 41040 | 4672 | | } |
| | 16690 | 4673 | | SolveAllTypeConstraints(); |
| | | 4674 | | |
| | 50070 | 4675 | | foreach (var cycle in dependencies.AllCycles()) { |
| | 0 | 4676 | | var cy = Util.Comma(" -> ", cycle, v => v.Name) + " -> " + cycle[0].Name; |
| | 0 | 4677 | | reporter.Error(MessageSource.Resolver, cycle[0], $"default-value expressions for parameters contain a cycle: {cy |
| | 0 | 4678 | | } |
| | 16690 | 4679 | | } |
| | | 4680 | | |
| | | 4681 | | /// <summary> |
| | | 4682 | | /// See ResolveTypeOption for a description of the option/defaultTypeArguments parameters. |
| | | 4683 | | /// </summary> |
| | 416250 | 4684 | | public void ResolveType(IToken tok, Type type, ResolutionContext resolutionContext, ResolveTypeOptionEnum eopt, List |
| | | 4685 | | Contract.Requires(tok != null); |
| | | 4686 | | Contract.Requires(type != null); |
| | | 4687 | | Contract.Requires(resolutionContext != null); |
| | | 4688 | | Contract.Requires(eopt != ResolveTypeOptionEnum.AllowPrefixExtend); |
| | 416250 | 4689 | | ResolveType(tok, type, resolutionContext, new ResolveTypeOption(eopt), defaultTypeArguments); |
| | 416250 | 4690 | | } |
| | | 4691 | | |
| | 840 | 4692 | | public void ResolveType(IToken tok, Type type, ICodeContext topLevelContext, ResolveTypeOptionEnum eopt, List<TypePa |
| | 840 | 4693 | | ResolveType(tok, type, ResolutionContext.FromCodeContext(topLevelContext), eopt, defaultTypeArguments); |
| | 840 | 4694 | | } |
| | | 4695 | | |
| | 60330 | 4696 | | public void ResolveType(IToken tok, Type type, ICodeContext topLevelContext, ResolveTypeOption option, List<TypePara |
| | 60330 | 4697 | | ResolveType(tok, type, ResolutionContext.FromCodeContext(topLevelContext), option, defaultTypeArguments); |
| | 60330 | 4698 | | } |
| | | 4699 | | |
| | 596340 | 4700 | | public void ResolveType(IToken tok, Type type, ResolutionContext resolutionContext, ResolveTypeOption option, List<T |
| | | 4701 | | Contract.Requires(tok != null); |
| | | 4702 | | Contract.Requires(type != null); |
| | | 4703 | | Contract.Requires(resolutionContext != null); |
| | | 4704 | | Contract.Requires(option != null); |
| | | 4705 | | Contract.Requires((option.Opt == ResolveTypeOptionEnum.DontInfer || option.Opt == ResolveTypeOptionEnum.InferTypeP |
| | 596340 | 4706 | | var r = ResolveTypeLenient(tok, type, resolutionContext, option, defaultTypeArguments, false); |
| | 596340 | 4707 | | Contract.Assert(r == null); |
| | 596340 | 4708 | | } |
| | | 4709 | | |
| | | 4710 | | public class ResolveTypeReturn { |
| | | 4711 | | public readonly Type ReplacementType; |
| | | 4712 | | public readonly ExprDotName LastComponent; |
| | 0 | 4713 | | public ResolveTypeReturn(Type replacementType, ExprDotName lastComponent) { |
| | | 4714 | | Contract.Requires(replacementType != null); |
| | | 4715 | | Contract.Requires(lastComponent != null); |
| | 0 | 4716 | | ReplacementType = replacementType; |
| | 0 | 4717 | | LastComponent = lastComponent; |
| | 0 | 4718 | | } |
| | | 4719 | | } |
| | | 4720 | | /// <summary> |
| | | 4721 | | /// See ResolveTypeOption for a description of the option/defaultTypeArguments parameters. |
| | | 4722 | | /// One more thing: if "allowDanglingDotName" is true, then if the resolution would have produced |
| | | 4723 | | /// an error message that could have been avoided if "type" denoted an identifier sequence one |
| | | 4724 | | /// shorter, then return an unresolved replacement type where the identifier sequence is one |
| | | 4725 | | /// shorter. (In all other cases, the method returns null.) |
| | | 4726 | | /// </summary> |
| | 596340 | 4727 | | public ResolveTypeReturn ResolveTypeLenient(IToken tok, Type type, ResolutionContext resolutionContext, ResolveTypeO |
| | | 4728 | | Contract.Requires(tok != null); |
| | | 4729 | | Contract.Requires(type != null); |
| | | 4730 | | Contract.Requires(resolutionContext != null); |
| | | 4731 | | Contract.Requires((option.Opt == ResolveTypeOptionEnum.DontInfer || option.Opt == ResolveTypeOptionEnum.InferTypeP |
| | 596340 | 4732 | | if (type is BitvectorType) { |
| | 0 | 4733 | | var t = (BitvectorType)type; |
| | | 4734 | | // nothing to resolve, but record the fact that this bitvector width is in use |
| | 0 | 4735 | | builtIns.Bitwidths.Add(t.Width); |
| | 692420 | 4736 | | } else if (type is BasicType) { |
| | | 4737 | | // nothing to resolve |
| | 596340 | 4738 | | } else if (type is MapType) { |
| | 0 | 4739 | | var mt = (MapType)type; |
| | 0 | 4740 | | var errorCount = reporter.Count(ErrorLevel.Error); |
| | | 4741 | | int typeArgumentCount; |
| | 0 | 4742 | | if (mt.HasTypeArg()) { |
| | 0 | 4743 | | ResolveType(tok, mt.Domain, resolutionContext, option, defaultTypeArguments); |
| | 0 | 4744 | | ResolveType(tok, mt.Range, resolutionContext, option, defaultTypeArguments); |
| | 0 | 4745 | | typeArgumentCount = 2; |
| | 0 | 4746 | | } else if (option.Opt == ResolveTypeOptionEnum.DontInfer) { |
| | 0 | 4747 | | mt.SetTypeArgs(new InferredTypeProxy(), new InferredTypeProxy()); |
| | 0 | 4748 | | typeArgumentCount = 0; |
| | 0 | 4749 | | } else { |
| | 0 | 4750 | | var inferredTypeArgs = new List<Type>(); |
| | 0 | 4751 | | FillInTypeArguments(tok, 2, inferredTypeArgs, defaultTypeArguments, option); |
| | 0 | 4752 | | Contract.Assert(inferredTypeArgs.Count <= 2); |
| | 0 | 4753 | | if (inferredTypeArgs.Count == 1) { |
| | 0 | 4754 | | mt.SetTypeArgs(inferredTypeArgs[0], new InferredTypeProxy()); |
| | 0 | 4755 | | typeArgumentCount = 1; |
| | 0 | 4756 | | } else if (inferredTypeArgs.Count == 2) { |
| | 0 | 4757 | | mt.SetTypeArgs(inferredTypeArgs[0], inferredTypeArgs[1]); |
| | 0 | 4758 | | typeArgumentCount = 2; |
| | 0 | 4759 | | } else { |
| | 0 | 4760 | | mt.SetTypeArgs(new InferredTypeProxy(), new InferredTypeProxy()); |
| | 0 | 4761 | | typeArgumentCount = 0; |
| | 0 | 4762 | | } |
| | 0 | 4763 | | } |
| | | 4764 | | // defaults and auto have been applied; check if we now have the right number of arguments |
| | 0 | 4765 | | if (2 != typeArgumentCount) { |
| | 0 | 4766 | | reporter.Error(MessageSource.Resolver, tok, "Wrong number of type arguments ({0} instead of 2) passed to type: |
| | 0 | 4767 | | } |
| | 560010 | 4768 | | } else if (type is CollectionType) { |
| | 59750 | 4769 | | var t = (CollectionType)type; |
| | 59750 | 4770 | | var errorCount = reporter.Count(ErrorLevel.Error); |
| | 119500 | 4771 | | if (t.HasTypeArg()) { |
| | 59750 | 4772 | | ResolveType(tok, t.Arg, resolutionContext, option, defaultTypeArguments); |
| | 59750 | 4773 | | } else if (option.Opt != ResolveTypeOptionEnum.DontInfer) { |
| | 0 | 4774 | | var inferredTypeArgs = new List<Type>(); |
| | 0 | 4775 | | FillInTypeArguments(tok, 1, inferredTypeArgs, defaultTypeArguments, option); |
| | 0 | 4776 | | if (inferredTypeArgs.Count != 0) { |
| | 0 | 4777 | | Contract.Assert(inferredTypeArgs.Count == 1); |
| | 0 | 4778 | | t.SetTypeArg(inferredTypeArgs[0]); |
| | 0 | 4779 | | } |
| | 0 | 4780 | | } |
| | 59750 | 4781 | | if (!t.HasTypeArg()) { |
| | | 4782 | | // defaults and auto have been applied; check if we now have the right number of arguments |
| | 0 | 4783 | | reporter.Error(MessageSource.Resolver, tok, "Wrong number of type arguments (0 instead of 1) passed to type: { |
| | | 4784 | | // add a proxy type, to make sure that CollectionType will have have a non-null Arg |
| | 0 | 4785 | | t.SetTypeArg(new InferredTypeProxy()); |
| | 0 | 4786 | | } |
| | | 4787 | | |
| | 612350 | 4788 | | } else if (type is UserDefinedType) { |
| | 112090 | 4789 | | var t = (UserDefinedType)type; |
| | 112510 | 4790 | | if (t.ResolvedClass != null) { |
| | | 4791 | | // Apparently, this type has already been resolved |
| | 420 | 4792 | | return null; |
| | | 4793 | | } |
| | 111670 | 4794 | | var prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | 111670 | 4795 | | if (t.NamePath is ExprDotName) { |
| | 0 | 4796 | | var ret = ResolveDotSuffix_Type((ExprDotName)t.NamePath, resolutionContext, allowDanglingDotName, option, defa |
| | 0 | 4797 | | if (ret != null) { |
| | 0 | 4798 | | return ret; |
| | | 4799 | | } |
| | 111670 | 4800 | | } else { |
| | 111670 | 4801 | | var s = (NameSegment)t.NamePath; |
| | 111670 | 4802 | | ResolveNameSegment_Type(s, resolutionContext, option, defaultTypeArguments); |
| | 111670 | 4803 | | } |
| | 223340 | 4804 | | if (reporter.Count(ErrorLevel.Error) == prevErrorCount) { |
| | 111670 | 4805 | | var r = t.NamePath.Resolved as Resolver_IdentifierExpr; |
| | 111670 | 4806 | | if (r == null || !(r.Type is Resolver_IdentifierExpr.ResolverType_Type)) { |
| | 0 | 4807 | | reporter.Error(MessageSource.Resolver, t.tok, "expected type"); |
| | 223340 | 4808 | | } else if (r.Type is Resolver_IdentifierExpr.ResolverType_Type) { |
| | 111670 | 4809 | | var d = r.Decl; |
| | 111670 | 4810 | | if (d is AbstractTypeDecl) { |
| | | 4811 | | // resolve like a type parameter, and it may have type parameters if it's an abstract type |
| | 0 | 4812 | | t.ResolvedClass = d; // Store the decl, so the compiler will generate the fully qualified name |
| | 171960 | 4813 | | } else if (d is RedirectingTypeDecl) { |
| | 60290 | 4814 | | var dd = (RedirectingTypeDecl)d; |
| | 60290 | 4815 | | var caller = CodeContextWrapper.Unwrap(resolutionContext.CodeContext) as ICallable; |
| | 119740 | 4816 | | if (caller != null && !(d is SubsetTypeDecl && caller is SpecialFunction)) { |
| | 118900 | 4817 | | if (caller != d) { |
| | 59450 | 4818 | | } else if (d is TypeSynonymDecl && !(d is SubsetTypeDecl)) { |
| | | 4819 | | // detect self-loops here, since they don't show up in the graph's SCC methods |
| | 0 | 4820 | | reporter.Error(MessageSource.Resolver, d.tok, "type-synonym cycle: {0} -> {0}", d.Name); |
| | 0 | 4821 | | } else { |
| | | 4822 | | // detect self-loops here, since they don't show up in the graph's SCC methods |
| | 0 | 4823 | | reporter.Error(MessageSource.Resolver, d.tok, "recursive constraint dependency involving a {0}: {1} -> |
| | 0 | 4824 | | } |
| | 59450 | 4825 | | } |
| | 60290 | 4826 | | t.ResolvedClass = d; |
| | 145410 | 4827 | | } else if (d is DatatypeDecl) { |
| | 33740 | 4828 | | t.ResolvedClass = d; |
| | 51380 | 4829 | | } else { |
| | | 4830 | | // d is a type parameter, coinductive datatype, or class, and it may have type parameters |
| | 17640 | 4831 | | t.ResolvedClass = d; |
| | 17640 | 4832 | | } |
| | 168300 | 4833 | | if (option.Opt == ResolveTypeOptionEnum.DontInfer) { |
| | | 4834 | | // don't add anything |
| | 111670 | 4835 | | } else if (d.TypeArgs.Count != t.TypeArgs.Count && t.TypeArgs.Count == 0) { |
| | 0 | 4836 | | FillInTypeArguments(t.tok, d.TypeArgs.Count, t.TypeArgs, defaultTypeArguments, option); |
| | 0 | 4837 | | } |
| | | 4838 | | // defaults and auto have been applied; check if we now have the right number of arguments |
| | 111670 | 4839 | | if (d.TypeArgs.Count != t.TypeArgs.Count) { |
| | 0 | 4840 | | reporter.Error(MessageSource.Resolver, t.tok, "Wrong number of type arguments ({0} instead of {1}) passed |
| | 0 | 4841 | | } |
| | | 4842 | | |
| | 111670 | 4843 | | } |
| | 111670 | 4844 | | } |
| | 111670 | 4845 | | if (t.ResolvedClass == null) { |
| | | 4846 | | // There was some error. Still, we will set .ResolvedClass to some value to prevent some crashes in the downst |
| | | 4847 | | // 0-tuple is convenient, because it is always in scope. |
| | 0 | 4848 | | t.ResolvedClass = builtIns.TupleType(t.tok, 0, false); |
| | | 4849 | | // clear out the TypeArgs since 0-tuple doesn't take TypeArg |
| | 0 | 4850 | | t.TypeArgs = new List<Type>(); |
| | 0 | 4851 | | } |
| | | 4852 | | |
| | 767670 | 4853 | | } else if (type is TypeProxy) { |
| | 327580 | 4854 | | TypeProxy t = (TypeProxy)type; |
| | 327580 | 4855 | | if (t.T != null) { |
| | 0 | 4856 | | ResolveType(tok, t.T, resolutionContext, option, defaultTypeArguments); |
| | 0 | 4857 | | } |
| | 329260 | 4858 | | } else if (type is SelfType) { |
| | | 4859 | | // do nothing. |
| | 840 | 4860 | | } else { |
| | 0 | 4861 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected type |
| | | 4862 | | } |
| | 595920 | 4863 | | return null; |
| | 596340 | 4864 | | } |
| | | 4865 | | |
| | | 4866 | | /// <summary> |
| | | 4867 | | /// Adds to "typeArgs" a list of "n" type arguments, possibly extending "defaultTypeArguments". |
| | | 4868 | | /// </summary> |
| | 0 | 4869 | | static void FillInTypeArguments(IToken tok, int n, List<Type> typeArgs, List<TypeParameter> defaultTypeArguments, Re |
| | | 4870 | | Contract.Requires(tok != null); |
| | | 4871 | | Contract.Requires(0 <= n); |
| | | 4872 | | Contract.Requires(typeArgs != null && typeArgs.Count == 0); |
| | 0 | 4873 | | if (option.Opt == ResolveTypeOptionEnum.InferTypeProxies) { |
| | | 4874 | | // add type arguments that will be inferred |
| | 0 | 4875 | | for (int i = 0; i < n; i++) { |
| | 0 | 4876 | | typeArgs.Add(new InferredTypeProxy()); |
| | 0 | 4877 | | } |
| | 0 | 4878 | | } else if (option.Opt == ResolveTypeOptionEnum.AllowPrefix && defaultTypeArguments.Count < n) { |
| | | 4879 | | // there aren't enough default arguments, so don't do anything |
| | 0 | 4880 | | } else { |
| | | 4881 | | // we'll add arguments |
| | 0 | 4882 | | if (option.Opt == ResolveTypeOptionEnum.AllowPrefixExtend) { |
| | | 4883 | | // extend defaultTypeArguments, if needed |
| | 0 | 4884 | | for (int i = defaultTypeArguments.Count; i < n; i++) { |
| | 0 | 4885 | | var tp = new TypeParameter(tok.ToRange(), new Name(tok.ToRange(), "_T" + i), i, option.Parent); |
| | 0 | 4886 | | if (option.Parent is IteratorDecl) { |
| | 0 | 4887 | | tp.Characteristics.AutoInit = Type.AutoInitInfo.CompilableValue; |
| | 0 | 4888 | | } |
| | 0 | 4889 | | defaultTypeArguments.Add(tp); |
| | 0 | 4890 | | } |
| | 0 | 4891 | | } |
| | 0 | 4892 | | Contract.Assert(n <= defaultTypeArguments.Count); |
| | | 4893 | | // automatically supply a prefix of the arguments from defaultTypeArguments |
| | 0 | 4894 | | for (int i = 0; i < n; i++) { |
| | 0 | 4895 | | typeArgs.Add(new UserDefinedType(defaultTypeArguments[i])); |
| | 0 | 4896 | | } |
| | 0 | 4897 | | } |
| | 0 | 4898 | | } |
| | | 4899 | | |
| | 840 | 4900 | | public static bool TypeConstraintsIncludeProxy(Type t, TypeProxy proxy) { |
| | 840 | 4901 | | return TypeConstraintsIncludeProxy_Aux(t, proxy, new HashSet<TypeProxy>()); |
| | 840 | 4902 | | } |
| | 1260 | 4903 | | static bool TypeConstraintsIncludeProxy_Aux(Type t, TypeProxy proxy, ISet<TypeProxy> visited) { |
| | | 4904 | | Contract.Requires(t != null); |
| | | 4905 | | Contract.Requires(!(t is TypeProxy) || ((TypeProxy)t).T == null); // t is expected to have been normalized first |
| | | 4906 | | Contract.Requires(proxy != null && proxy.T == null); |
| | | 4907 | | Contract.Requires(visited != null); |
| | 1260 | 4908 | | var tproxy = t as TypeProxy; |
| | 1260 | 4909 | | if (tproxy != null) { |
| | 0 | 4910 | | if (object.ReferenceEquals(tproxy, proxy)) { |
| | 0 | 4911 | | return true; |
| | 0 | 4912 | | } else if (visited.Contains(tproxy)) { |
| | 0 | 4913 | | return false; |
| | | 4914 | | } |
| | 0 | 4915 | | visited.Add(tproxy); |
| | 0 | 4916 | | foreach (var su in tproxy.Subtypes) { |
| | 0 | 4917 | | if (TypeConstraintsIncludeProxy_Aux(su, proxy, visited)) { |
| | 0 | 4918 | | return true; |
| | | 4919 | | } |
| | 0 | 4920 | | } |
| | 0 | 4921 | | foreach (var su in tproxy.Supertypes) { |
| | 0 | 4922 | | if (TypeConstraintsIncludeProxy_Aux(su, proxy, visited)) { |
| | 0 | 4923 | | return true; |
| | | 4924 | | } |
| | 0 | 4925 | | } |
| | 1260 | 4926 | | } else { |
| | | 4927 | | // check type arguments of t |
| | 5040 | 4928 | | foreach (var ta in t.TypeArgs) { |
| | 420 | 4929 | | var a = ta.Normalize(); |
| | 420 | 4930 | | if (TypeConstraintsIncludeProxy_Aux(a, proxy, visited)) { |
| | 0 | 4931 | | return true; |
| | | 4932 | | } |
| | 420 | 4933 | | } |
| | 1260 | 4934 | | } |
| | 1260 | 4935 | | return false; |
| | 1260 | 4936 | | } |
| | | 4937 | | |
| | 0 | 4938 | | public Type ResolveTypeRhs(TypeRhs rr, Statement stmt, ResolutionContext resolutionContext) { |
| | | 4939 | | Contract.Requires(rr != null); |
| | | 4940 | | Contract.Requires(stmt != null); |
| | | 4941 | | Contract.Requires(resolutionContext != null); |
| | | 4942 | | Contract.Ensures(Contract.Result<Type>() != null); |
| | | 4943 | | |
| | 0 | 4944 | | if (rr.Type == null) { |
| | 0 | 4945 | | if (rr.ArrayDimensions != null) { |
| | | 4946 | | // ---------- new T[EE] OR new T[EE] (elementInit) |
| | 0 | 4947 | | Contract.Assert(rr.Bindings == null && rr.Path == null && rr.InitCall == null); |
| | 0 | 4948 | | ResolveType(stmt.Tok, rr.EType, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 4949 | | int i = 0; |
| | 0 | 4950 | | foreach (Expression dim in rr.ArrayDimensions) { |
| | 0 | 4951 | | Contract.Assert(dim != null); |
| | 0 | 4952 | | ResolveExpression(dim, resolutionContext); |
| | 0 | 4953 | | ConstrainToIntegerType(dim, false, string.Format("new must use an integer-based expression for the array siz |
| | 0 | 4954 | | i++; |
| | 0 | 4955 | | } |
| | 0 | 4956 | | rr.Type = ResolvedArrayType(stmt.Tok, rr.ArrayDimensions.Count, rr.EType, resolutionContext, false); |
| | 0 | 4957 | | if (rr.ElementInit != null) { |
| | 0 | 4958 | | ResolveExpression(rr.ElementInit, resolutionContext); |
| | | 4959 | | // Check |
| | | 4960 | | // int^N -> rr.EType :> rr.ElementInit.Type |
| | 0 | 4961 | | builtIns.CreateArrowTypeDecl(rr.ArrayDimensions.Count); // TODO: should this be done already in the parser? |
| | 0 | 4962 | | var args = new List<Type>(); |
| | 0 | 4963 | | for (int ii = 0; ii < rr.ArrayDimensions.Count; ii++) { |
| | 0 | 4964 | | args.Add(builtIns.Nat()); |
| | 0 | 4965 | | } |
| | 0 | 4966 | | var arrowType = new ArrowType(rr.ElementInit.tok, builtIns.ArrowTypeDecls[rr.ArrayDimensions.Count], args, r |
| | 0 | 4967 | | var lambdaType = rr.ElementInit.Type.AsArrowType; |
| | 0 | 4968 | | if (lambdaType != null && lambdaType.TypeArgs[0] is InferredTypeProxy) { |
| | 0 | 4969 | | (lambdaType.TypeArgs[0] as InferredTypeProxy).KeepConstraints = true; |
| | 0 | 4970 | | } |
| | | 4971 | | string underscores; |
| | 0 | 4972 | | if (rr.ArrayDimensions.Count == 1) { |
| | 0 | 4973 | | underscores = "_"; |
| | 0 | 4974 | | } else { |
| | 0 | 4975 | | underscores = "(" + Util.Comma(rr.ArrayDimensions.Count, x => "_") + ")"; |
| | 0 | 4976 | | } |
| | 0 | 4977 | | var hintString = string.Format(" (perhaps write '{0} =>' in front of the expression you gave in order to mak |
| | 0 | 4978 | | ConstrainSubtypeRelation(arrowType, rr.ElementInit.Type, rr.ElementInit, "array-allocation initialization ex |
| | 0 | 4979 | | arrowType, rr.ElementInit.Type, new LazyString_OnTypeEquals(rr.EType, rr.ElementInit.Type, hintString)); |
| | 0 | 4980 | | } else if (rr.InitDisplay != null) { |
| | 0 | 4981 | | foreach (var v in rr.InitDisplay) { |
| | 0 | 4982 | | ResolveExpression(v, resolutionContext); |
| | 0 | 4983 | | AddAssignableConstraint(v.tok, rr.EType, v.Type, "initial value must be assignable to array's elements (ex |
| | 0 | 4984 | | } |
| | 0 | 4985 | | } |
| | 0 | 4986 | | } else { |
| | 0 | 4987 | | bool callsConstructor = false; |
| | 0 | 4988 | | if (rr.Bindings == null) { |
| | 0 | 4989 | | ResolveType(stmt.Tok, rr.EType, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 4990 | | var cl = (rr.EType as UserDefinedType)?.ResolvedClass as NonNullTypeDecl; |
| | 0 | 4991 | | if (cl != null && !(rr.EType.IsTraitType && !rr.EType.NormalizeExpand().IsObjectQ)) { |
| | | 4992 | | // life is good |
| | 0 | 4993 | | } else { |
| | 0 | 4994 | | reporter.Error(MessageSource.Resolver, rr.tok, "new can be applied only to class types (got {0})", rr.ETyp |
| | 0 | 4995 | | } |
| | 0 | 4996 | | } else { |
| | 0 | 4997 | | string initCallName = null; |
| | 0 | 4998 | | IToken initCallTok = null; |
| | | 4999 | | // Resolve rr.Path and do one of three things: |
| | | 5000 | | // * If rr.Path denotes a type, then set EType,initCallName to rr.Path,"_ctor", which sets up a call to the |
| | | 5001 | | // * If the all-but-last components of rr.Path denote a type, then do EType,initCallName := allButLast(EType |
| | | 5002 | | // * Otherwise, report an error |
| | 0 | 5003 | | var ret = ResolveTypeLenient(rr.Tok, rr.Path, resolutionContext, new ResolveTypeOption(ResolveTypeOptionEnum |
| | 0 | 5004 | | if (ret != null) { |
| | | 5005 | | // The all-but-last components of rr.Path denote a type (namely, ret.ReplacementType). |
| | 0 | 5006 | | rr.EType = ret.ReplacementType; |
| | 0 | 5007 | | initCallName = ret.LastComponent.SuffixName; |
| | 0 | 5008 | | initCallTok = ret.LastComponent.tok; |
| | 0 | 5009 | | } else { |
| | | 5010 | | // Either rr.Path resolved correctly as a type or there was no way to drop a last component to make it int |
| | | 5011 | | // like a type. In either case, set EType,initCallName to Path,"_ctor" and continue. |
| | 0 | 5012 | | rr.EType = rr.Path; |
| | 0 | 5013 | | initCallName = "_ctor"; |
| | 0 | 5014 | | initCallTok = rr.Tok; |
| | 0 | 5015 | | } |
| | 0 | 5016 | | var cl = (rr.EType as UserDefinedType)?.ResolvedClass as NonNullTypeDecl; |
| | 0 | 5017 | | if (cl == null || rr.EType.IsTraitType) { |
| | 0 | 5018 | | reporter.Error(MessageSource.Resolver, rr.tok, "new can be applied only to class types (got {0})", rr.ETyp |
| | 0 | 5019 | | } else { |
| | | 5020 | | // ---------- new C.Init(EE) |
| | 0 | 5021 | | Contract.Assert(initCallName != null); |
| | 0 | 5022 | | var prevErrorCount = reporter.Count(ErrorLevel.Error); |
| | | 5023 | | |
| | | 5024 | | // We want to create a MemberSelectExpr for the initializing method. To do that, we create a throw-away r |
| | | 5025 | | // type, create a dot-suffix expression around this receiver, and then resolve it in the usual way for dot |
| | 0 | 5026 | | var lhs = new ImplicitThisExpr_ConstructorCall(initCallTok) { Type = rr.EType }; |
| | 0 | 5027 | | var callLhs = new ExprDotName(((UserDefinedType)rr.EType).tok, lhs, initCallName, ret == null ? null : ret |
| | 0 | 5028 | | ResolveDotSuffix(callLhs, true, rr.Bindings.ArgumentBindings, resolutionContext, true); |
| | 0 | 5029 | | if (prevErrorCount == reporter.Count(ErrorLevel.Error)) { |
| | 0 | 5030 | | Contract.Assert(callLhs.ResolvedExpression is MemberSelectExpr); // since ResolveApplySuffix succeeded |
| | 0 | 5031 | | var methodSel = (MemberSelectExpr)callLhs.ResolvedExpression; |
| | 0 | 5032 | | if (methodSel.Member is Method) { |
| | 0 | 5033 | | rr.InitCall = new CallStmt(stmt.RangeToken, new List<Expression>(), methodSel, rr.Bindings.ArgumentBin |
| | 0 | 5034 | | ResolveCallStmt(rr.InitCall, resolutionContext, rr.EType); |
| | 0 | 5035 | | if (rr.InitCall.Method is Constructor) { |
| | 0 | 5036 | | callsConstructor = true; |
| | 0 | 5037 | | } |
| | 0 | 5038 | | } else { |
| | 0 | 5039 | | reporter.Error(MessageSource.Resolver, initCallTok, "object initialization must denote an initializing |
| | 0 | 5040 | | } |
| | 0 | 5041 | | } |
| | 0 | 5042 | | } |
| | 0 | 5043 | | } |
| | 0 | 5044 | | if (rr.EType.IsRefType) { |
| | 0 | 5045 | | var udt = rr.EType.NormalizeExpand() as UserDefinedType; |
| | 0 | 5046 | | if (udt != null) { |
| | 0 | 5047 | | var cl = (ClassDecl)udt.ResolvedClass; // cast is guaranteed by the call to rr.EType.IsRefType above, tog |
| | 0 | 5048 | | if (!callsConstructor && !cl.IsObjectTrait && !udt.IsArrayType && (cl.HasConstructor || cl.EnclosingModule |
| | 0 | 5049 | | reporter.Error(MessageSource.Resolver, stmt, "when allocating an object of {1}type '{0}', one of its con |
| | 0 | 5050 | | cl.HasConstructor ? "" : "imported "); |
| | 0 | 5051 | | } |
| | 0 | 5052 | | } |
| | 0 | 5053 | | } |
| | 0 | 5054 | | rr.Type = rr.EType; |
| | 0 | 5055 | | } |
| | 0 | 5056 | | } |
| | 0 | 5057 | | return rr.Type; |
| | 0 | 5058 | | } |
| | | 5059 | | |
| | | 5060 | | /// <summary> |
| | | 5061 | | /// Resolve "memberName" in what currently is known as "receiverType". If "receiverType" is an unresolved |
| | | 5062 | | /// proxy type, try to solve enough type constraints and use heuristics to figure out which type contains |
| | | 5063 | | /// "memberName" and return that enclosing type as "tentativeReceiverType". However, try not to make |
| | | 5064 | | /// type-inference decisions about "receiverType"; instead, lay down the further constraints that need to |
| | | 5065 | | /// be satisfied in order for "tentativeReceiverType" to be where "memberName" is found. |
| | | 5066 | | /// Consequently, if "memberName" is found and returned as a "MemberDecl", it may still be the case that |
| | | 5067 | | /// "receiverType" is an unresolved proxy type and that, after solving more type constraints, "receiverType" |
| | | 5068 | | /// eventually gets set to a type more specific than "tentativeReceiverType". |
| | | 5069 | | /// </summary> |
| | 8050 | 5070 | | public MemberDecl ResolveMember(IToken tok, Type receiverType, string memberName, out NonProxyType tentativeReceiver |
| | | 5071 | | Contract.Requires(tok != null); |
| | | 5072 | | Contract.Requires(receiverType != null); |
| | | 5073 | | Contract.Requires(memberName != null); |
| | | 5074 | | Contract.Ensures(Contract.Result<MemberDecl>() == null || Contract.ValueAtReturn(out tentativeReceiverType) != nul |
| | | 5075 | | |
| | 8050 | 5076 | | receiverType = PartiallyResolveTypeForMemberSelection(tok, receiverType, memberName); |
| | | 5077 | | |
| | 8050 | 5078 | | if (receiverType is TypeProxy) { |
| | 0 | 5079 | | reporter.Error(MessageSource.Resolver, tok, "type of the receiver is not fully determined at this program point" |
| | 0 | 5080 | | tentativeReceiverType = null; |
| | 0 | 5081 | | return null; |
| | | 5082 | | } |
| | 8050 | 5083 | | Contract.Assert(receiverType is NonProxyType); // there are only two kinds of types: proxies and non-proxies |
| | | 5084 | | |
| | 193200 | 5085 | | foreach (var valuet in valuetypeDecls) { |
| | 56350 | 5086 | | if (valuet.IsThisType(receiverType)) { |
| | 0 | 5087 | | if (valuet.Members.TryGetValue(memberName, out var member)) { |
| | 0 | 5088 | | SelfType resultType = null; |
| | 0 | 5089 | | if (member is SpecialFunction) { |
| | 0 | 5090 | | resultType = ((SpecialFunction)member).ResultType as SelfType; |
| | 0 | 5091 | | } else if (member is SpecialField) { |
| | 0 | 5092 | | resultType = ((SpecialField)member).Type as SelfType; |
| | 0 | 5093 | | } |
| | 0 | 5094 | | if (resultType != null) { |
| | 0 | 5095 | | SelfTypeSubstitution = new Dictionary<TypeParameter, Type>(); |
| | 0 | 5096 | | SelfTypeSubstitution.Add(resultType.TypeArg, receiverType); |
| | 0 | 5097 | | resultType.ResolvedType = receiverType; |
| | 0 | 5098 | | } |
| | 0 | 5099 | | tentativeReceiverType = (NonProxyType)receiverType; |
| | 0 | 5100 | | return member; |
| | | 5101 | | } |
| | 0 | 5102 | | break; |
| | | 5103 | | } |
| | 56350 | 5104 | | } |
| | | 5105 | | |
| | 8050 | 5106 | | var ctype = receiverType.NormalizeExpand() as UserDefinedType; |
| | 8050 | 5107 | | var cd = ctype?.AsTopLevelTypeWithMembersBypassInternalSynonym; |
| | 16100 | 5108 | | if (cd != null) { |
| | 8050 | 5109 | | Contract.Assert(ctype.TypeArgs.Count == cd.TypeArgs.Count); // follows from the fact that ctype was resolved |
| | 8050 | 5110 | | if (!classMembers[cd].TryGetValue(memberName, out var member)) { |
| | 0 | 5111 | | if (memberName == "_ctor") { |
| | 0 | 5112 | | reporter.Error(MessageSource.Resolver, tok, "{0} {1} does not have an anonymous constructor", cd.WhatKind, c |
| | 0 | 5113 | | } else { |
| | 0 | 5114 | | reporter.Error(MessageSource.Resolver, tok, "member '{0}' does not exist in {2} '{1}'", memberName, cd.Name, |
| | 0 | 5115 | | } |
| | 8050 | 5116 | | } else if (!VisibleInScope(member)) { |
| | 0 | 5117 | | reporter.Error(MessageSource.Resolver, tok, "member '{0}' has not been imported in this scope and cannot be ac |
| | 8050 | 5118 | | } else { |
| | 8050 | 5119 | | tentativeReceiverType = ctype; |
| | 8050 | 5120 | | return member; |
| | | 5121 | | } |
| | 0 | 5122 | | tentativeReceiverType = null; |
| | 0 | 5123 | | return null; |
| | | 5124 | | } |
| | | 5125 | | |
| | 0 | 5126 | | reporter.Error(MessageSource.Resolver, tok, "type {0} does not have a member {1}", receiverType, memberName); |
| | 0 | 5127 | | tentativeReceiverType = null; |
| | 0 | 5128 | | return null; |
| | 8050 | 5129 | | } |
| | | 5130 | | |
| | | 5131 | | /// <summary> |
| | | 5132 | | /// Roughly speaking, tries to figure out the head of the type of "t", making as few inference decisions as possible |
| | | 5133 | | /// More precisely, returns a type that contains all the members of "t"; or if "memberName" is non-null, a type |
| | | 5134 | | /// that at least contains the member "memberName" of "t". Typically, this type is the head type of "t", |
| | | 5135 | | /// but it may also be a type in a super- or subtype relation to "t". |
| | | 5136 | | /// In some cases, it is necessary to make some inference decisions in order to figure out the type to return. |
| | | 5137 | | /// </summary> |
| | 55220 | 5138 | | public Type PartiallyResolveTypeForMemberSelection(IToken tok, Type t, string memberName = null, int strength = 0) { |
| | | 5139 | | Contract.Requires(tok != null); |
| | | 5140 | | Contract.Requires(t != null); |
| | | 5141 | | Contract.Ensures(Contract.Result<Type>() != null); |
| | | 5142 | | Contract.Ensures(!(Contract.Result<Type>() is TypeProxy) || ((TypeProxy)Contract.Result<Type>()).T == null); |
| | 55220 | 5143 | | t = t.NormalizeExpand(); |
| | 70280 | 5144 | | if (!(t is TypeProxy)) { |
| | 15060 | 5145 | | return t; // we're good |
| | | 5146 | | } |
| | | 5147 | | |
| | | 5148 | | // simplify constraints |
| | 40160 | 5149 | | PrintTypeConstraintState(10); |
| | 60760 | 5150 | | if (strength > 0) { |
| | 20600 | 5151 | | var proxySpecializations = new HashSet<TypeProxy>(); |
| | 20600 | 5152 | | GetRelatedTypeProxies(t, proxySpecializations); |
| | 20600 | 5153 | | var anyNewConstraintsAssignable = ConvertAssignableToSubtypeConstraints(proxySpecializations); |
| | 20600 | 5154 | | var anyNewConstraintsEquatable = TightenUpEquatable(proxySpecializations); |
| | 30760 | 5155 | | if ((strength > 1 && !anyNewConstraintsAssignable && !anyNewConstraintsEquatable) || strength == 10) { |
| | 20320 | 5156 | | if (t is TypeProxy) { |
| | | 5157 | | // One more try |
| | 10160 | 5158 | | var r = GetBaseTypeFromProxy((TypeProxy)t, new Dictionary<TypeProxy, Type>()); |
| | 10200 | 5159 | | if (r != null) { |
| | 40 | 5160 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5161 | | Options.OutputWriter.WriteLine(" ----> found improvement through GetBaseTypeFromProxy: {0}", r); |
| | 0 | 5162 | | } |
| | 40 | 5163 | | return r; |
| | | 5164 | | } |
| | 10120 | 5165 | | } |
| | | 5166 | | |
| | 10120 | 5167 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5168 | | Options.OutputWriter.WriteLine(" ----> found no improvement, giving up"); |
| | 0 | 5169 | | } |
| | 10120 | 5170 | | return t; |
| | | 5171 | | } |
| | 10440 | 5172 | | } |
| | 30000 | 5173 | | PartiallySolveTypeConstraints(false); |
| | 30000 | 5174 | | PrintTypeConstraintState(11); |
| | 30000 | 5175 | | t = t.NormalizeExpandKeepConstraints(); |
| | 30000 | 5176 | | var proxy = t as TypeProxy; |
| | 30020 | 5177 | | if (proxy == null) { |
| | 20 | 5178 | | return t; // simplification did the trick |
| | | 5179 | | } |
| | 29980 | 5180 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5181 | | Options.OutputWriter.WriteLine("DEBUG: Member selection{3}: {1} :> {0} :> {2}", t, |
| | 0 | 5182 | | Util.Comma(proxy.SupertypesKeepConstraints, su => su.ToString()), |
| | 0 | 5183 | | Util.Comma(proxy.SubtypesKeepConstraints, su => su.ToString()), |
| | 0 | 5184 | | memberName == null ? "" : " (" + memberName + ")"); |
| | 0 | 5185 | | } |
| | | 5186 | | |
| | | 5187 | | // Look for a join of head symbols among the proxy's subtypes |
| | 29980 | 5188 | | Type joinType = null; |
| | 37440 | 5189 | | if (JoinOfAllSubtypes(proxy, ref joinType, new HashSet<TypeProxy>()) && joinType != null) { |
| | 7460 | 5190 | | DetermineRootLeaf(joinType, out _, out _, out var headIsRoot, out _); |
| | 13180 | 5191 | | if (joinType.IsDatatype) { |
| | 5720 | 5192 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5193 | | Options.OutputWriter.WriteLine(" ----> join is a datatype: {0}", joinType); |
| | 0 | 5194 | | } |
| | 5720 | 5195 | | ConstrainSubtypeRelation(t, joinType, tok, "Member selection requires a supertype of {0} (got something more l |
| | 5720 | 5196 | | return joinType; |
| | 3130 | 5197 | | } else if (headIsRoot) { |
| | | 5198 | | // we're good to go -- by picking "join" (whose type parameters have been replaced by fresh proxies), we're no |
| | 1390 | 5199 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5200 | | Options.OutputWriter.WriteLine(" ----> improved to {0} through join", joinType); |
| | 0 | 5201 | | } |
| | 1390 | 5202 | | AssignProxyAndHandleItsConstraints(proxy, joinType, true); |
| | 1390 | 5203 | | return proxy.NormalizeExpand(); // we return proxy.T instead of join, in case the assignment gets hijacked |
| | 700 | 5204 | | } else if (memberName == "_#apply" || memberName == "requires" || memberName == "reads") { |
| | 350 | 5205 | | var generalArrowType = joinType.AsArrowType; // go all the way to the base type, to get to the general arrow |
| | 700 | 5206 | | if (generalArrowType != null) { |
| | | 5207 | | // pick the supertype "generalArrowType" of "join" |
| | 350 | 5208 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5209 | | Options.OutputWriter.WriteLine(" ----> improved to {0} through join and function application", generalArr |
| | 0 | 5210 | | } |
| | 350 | 5211 | | ConstrainSubtypeRelation(generalArrowType, t, tok, "Function application requires a subtype of {0} (got some |
| | 350 | 5212 | | return generalArrowType; |
| | | 5213 | | } |
| | 0 | 5214 | | } else if (memberName != null) { |
| | | 5215 | | // If "join" has a member called "memberName" and no supertype of "join" does, then we'll pick this join |
| | 0 | 5216 | | if (joinType.IsRefType) { |
| | 0 | 5217 | | var joinExpanded = joinType.NormalizeExpand(); // go all the way to the base type, to get to the class |
| | 0 | 5218 | | if (!joinExpanded.IsObjectQ) { |
| | 0 | 5219 | | var cl = ((UserDefinedType)joinExpanded).ResolvedClass as ClassDecl; |
| | 0 | 5220 | | if (cl != null) { |
| | | 5221 | | // TODO: the following could be improved by also supplying an upper bound of the search (computed as a j |
| | 0 | 5222 | | var plausibleMembers = new HashSet<MemberDecl>(); |
| | 0 | 5223 | | FindAllMembers(cl, memberName, plausibleMembers); |
| | 0 | 5224 | | if (plausibleMembers.Count == 1) { |
| | 0 | 5225 | | var mbr = plausibleMembers.First(); |
| | 0 | 5226 | | if (mbr.EnclosingClass == cl) { |
| | 0 | 5227 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5228 | | Options.OutputWriter.WriteLine(" ----> improved to {0} through member-selection join", joinType); |
| | 0 | 5229 | | } |
| | 0 | 5230 | | var joinRoot = joinType.NormalizeExpand(); // blow passed any constraints |
| | 0 | 5231 | | ConstrainSubtypeRelation(joinRoot, t, tok, "Member selection requires a subtype of {0} (got somethin |
| | 0 | 5232 | | return joinType; |
| | 0 | 5233 | | } else { |
| | | 5234 | | // pick the supertype "mbr.EnclosingClass" of "cl" |
| | 0 | 5235 | | Contract.Assert(mbr.EnclosingClass is TraitDecl); // a proper supertype of a ClassDecl must be a Tr |
| | 0 | 5236 | | var typeMapping = cl.ParentFormalTypeParametersToActuals; |
| | 0 | 5237 | | TopLevelDecl td = mbr.EnclosingClass; |
| | 0 | 5238 | | foreach (var tt in cl.TraitAncestors()) { |
| | | 5239 | | // If there is a match, the list of Type actuals is unique |
| | | 5240 | | // (a class cannot inherit both Trait<T1> and Trait<T2> with T1 != T2). |
| | 0 | 5241 | | if (tt == (TraitDecl)mbr.EnclosingClass) { |
| | 0 | 5242 | | td = tt; |
| | 0 | 5243 | | } |
| | 0 | 5244 | | } |
| | 0 | 5245 | | List<Type> proxyTypeArgs = td.TypeArgs.ConvertAll(t0 => typeMapping.ContainsKey(t0) ? typeMapping[t0 |
| | 0 | 5246 | | var joinMapping = TypeParameter.SubstitutionMap(cl.TypeArgs, joinType.TypeArgs); |
| | 0 | 5247 | | proxyTypeArgs = proxyTypeArgs.ConvertAll(t0 => t0.Subst(joinMapping)); |
| | 0 | 5248 | | proxyTypeArgs = proxyTypeArgs.ConvertAll(t0 => t0.AsTypeParameter == null ? t0 : (Type)new InferredT |
| | 0 | 5249 | | var pickItFromHere = new UserDefinedType(tok, mbr.EnclosingClass.Name, mbr.EnclosingClass, proxyType |
| | 0 | 5250 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5251 | | Options.OutputWriter.WriteLine(" ----> improved to {0} through join and member lookup", pickItFro |
| | 0 | 5252 | | } |
| | 0 | 5253 | | ConstrainSubtypeRelation(pickItFromHere, t, tok, "Member selection requires a subtype of {0} (got so |
| | 0 | 5254 | | return pickItFromHere; |
| | | 5255 | | } |
| | | 5256 | | } |
| | 0 | 5257 | | } |
| | 0 | 5258 | | } |
| | 0 | 5259 | | } |
| | 0 | 5260 | | } |
| | 0 | 5261 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5262 | | Options.OutputWriter.WriteLine(" ----> found no improvement, because join does not determine type enough"); |
| | 0 | 5263 | | } |
| | 0 | 5264 | | } |
| | | 5265 | | |
| | | 5266 | | // Compute the meet of the proxy's supertypes |
| | 22520 | 5267 | | Type meet = null; |
| | 23950 | 5268 | | if (MeetOfAllSupertypes(proxy, ref meet, new HashSet<TypeProxy>(), false) && meet != null) { |
| | | 5269 | | // If the meet does have the member, then this looks promising. It could be that the |
| | | 5270 | | // type would get further constrained later to pick some subtype (in particular, a |
| | | 5271 | | // subclass that overrides the member) of this meet. But this is the best we can do |
| | | 5272 | | // now. |
| | 1790 | 5273 | | if (meet is TypeProxy) { |
| | 360 | 5274 | | if (proxy == meet.Normalize()) { |
| | | 5275 | | // can this really ever happen? |
| | 0 | 5276 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5277 | | Options.OutputWriter.WriteLine(" ----> found no improvement (other than the proxy itself)"); |
| | 0 | 5278 | | } |
| | 0 | 5279 | | return t; |
| | 360 | 5280 | | } else { |
| | 360 | 5281 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5282 | | Options.OutputWriter.WriteLine(" ----> (merging, then trying to improve further) assigning proxy {0}.T := |
| | 0 | 5283 | | } |
| | 360 | 5284 | | Contract.Assert(proxy != meet); |
| | 360 | 5285 | | proxy.T = meet; |
| | 360 | 5286 | | Contract.Assert(t.NormalizeExpand() == meet); |
| | 360 | 5287 | | return PartiallyResolveTypeForMemberSelection(tok, t, memberName, strength + 1); |
| | | 5288 | | } |
| | | 5289 | | } |
| | 1180 | 5290 | | if (!(meet is ArtificialType)) { |
| | 110 | 5291 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5292 | | Options.OutputWriter.WriteLine(" ----> improved to {0} through meet", meet); |
| | 0 | 5293 | | } |
| | 110 | 5294 | | if (memberName != null) { |
| | 0 | 5295 | | AssignProxyAndHandleItsConstraints(proxy, meet, true); |
| | 0 | 5296 | | return proxy.NormalizeExpand(); // we return proxy.T instead of meet, in case the assignment gets hijacked |
| | 110 | 5297 | | } else { |
| | 110 | 5298 | | return meet; |
| | | 5299 | | } |
| | | 5300 | | } |
| | 960 | 5301 | | } |
| | | 5302 | | |
| | | 5303 | | // as a last resort, act on any artificial type nearby the proxy |
| | 22050 | 5304 | | var artificialSuper = proxy.InClusterOfArtificial(AllXConstraints); |
| | 23860 | 5305 | | if (artificialSuper != null) { |
| | 1810 | 5306 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5307 | | Options.OutputWriter.WriteLine(" ----> use artificial supertype: {0}", artificialSuper); |
| | 0 | 5308 | | } |
| | 1810 | 5309 | | return artificialSuper; |
| | | 5310 | | } |
| | | 5311 | | |
| | | 5312 | | // we weren't able to do it |
| | 20240 | 5313 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5314 | | Options.OutputWriter.WriteLine(" ----> found no improvement using simple things, trying harder once more"); |
| | 0 | 5315 | | } |
| | 20240 | 5316 | | return PartiallyResolveTypeForMemberSelection(tok, t, memberName, strength + 1); |
| | 55220 | 5317 | | } |
| | | 5318 | | |
| | 10240 | 5319 | | private Type/*?*/ GetBaseTypeFromProxy(TypeProxy proxy, Dictionary<TypeProxy, Type/*?*/> determinedProxies) { |
| | | 5320 | | Contract.Requires(proxy != null); |
| | | 5321 | | Contract.Requires(determinedProxies != null); |
| | 10260 | 5322 | | if (determinedProxies.TryGetValue(proxy, out var t)) { |
| | | 5323 | | // "t" may be null (meaning search for "proxy" is underway or was unsuccessful) or non-null (search for |
| | | 5324 | | // "proxy" has completed successfully), but we return it in either case |
| | 20 | 5325 | | return t; |
| | | 5326 | | } |
| | 10220 | 5327 | | determinedProxies.Add(proxy, null); // record that search for "proxy" is underway |
| | | 5328 | | // First, go through subtype constraints, treating each as if it were an equality |
| | 16594270 | 5329 | | foreach (var c in AllTypeConstraints) { |
| | 5521220 | 5330 | | t = GetBaseTypeFromProxy_Eq(proxy, c.Super, c.Sub, determinedProxies); |
| | 5521270 | 5331 | | if (t != null) { |
| | 50 | 5332 | | determinedProxies[proxy] = t; |
| | 50 | 5333 | | return t; |
| | | 5334 | | } |
| | 5521170 | 5335 | | } |
| | | 5336 | | // Next, check XConstraints that can be seen as equality constraints |
| | 26869210 | 5337 | | foreach (var xc in AllXConstraints) { |
| | 8946250 | 5338 | | switch (xc.ConstraintName) { |
| | | 5339 | | case "Assignable": |
| | | 5340 | | case "Equatable": |
| | | 5341 | | case "EquatableArg": |
| | 8452000 | 5342 | | t = GetBaseTypeFromProxy_Eq(proxy, xc.Types[0], xc.Types[1], determinedProxies); |
| | 8452050 | 5343 | | if (t != null) { |
| | 50 | 5344 | | determinedProxies[proxy] = t; |
| | 50 | 5345 | | return t; |
| | | 5346 | | } |
| | 8451950 | 5347 | | break; |
| | | 5348 | | case "InSet": |
| | | 5349 | | // etc. TODO |
| | 0 | 5350 | | break; |
| | | 5351 | | default: |
| | 494250 | 5352 | | break; |
| | | 5353 | | } |
| | 8946200 | 5354 | | } |
| | 10120 | 5355 | | return null; |
| | 10240 | 5356 | | } |
| | | 5357 | | /// <summary> |
| | | 5358 | | /// Tries to find a non-proxy type corresponding to "proxy", under the assumption that "t" equals "u" and |
| | | 5359 | | /// "determinedProxies" assumptions. In the process, may add to "determinedProxies". |
| | | 5360 | | /// </summary> |
| | 13973220 | 5361 | | private Type/*?*/ GetBaseTypeFromProxy_Eq(TypeProxy proxy, Type t, Type u, Dictionary<TypeProxy, Type/*?*/> determin |
| | | 5362 | | Contract.Requires(proxy != null); |
| | | 5363 | | Contract.Requires(determinedProxies != null); |
| | | 5364 | | Contract.Requires(t != null); |
| | | 5365 | | Contract.Requires(u != null); |
| | 13973220 | 5366 | | t = t.NormalizeExpand(); |
| | 13973220 | 5367 | | u = u.NormalizeExpand(); |
| | 13973220 | 5368 | | return GetBaseTypeFromProxy_EqAux(proxy, t, u, determinedProxies) ?? GetBaseTypeFromProxy_EqAux(proxy, u, t, deter |
| | 13973220 | 5369 | | } |
| | 27946380 | 5370 | | private Type/*?*/ GetBaseTypeFromProxy_EqAux(TypeProxy proxy, Type t, Type u, Dictionary<TypeProxy, Type/*?*/> deter |
| | | 5371 | | Contract.Requires(proxy != null); |
| | | 5372 | | Contract.Requires(determinedProxies != null); |
| | | 5373 | | Contract.Requires(t != null && (!(t is TypeProxy) || ((TypeProxy)t).T == null)); |
| | | 5374 | | Contract.Requires(u != null && (!(u is TypeProxy) || ((TypeProxy)u).T == null)); |
| | 27946500 | 5375 | | if (t == proxy) { |
| | 200 | 5376 | | if (u is TypeProxy) { |
| | 80 | 5377 | | return GetBaseTypeFromProxy((TypeProxy)u, determinedProxies); |
| | 40 | 5378 | | } else { |
| | 40 | 5379 | | return u; |
| | | 5380 | | } |
| | 27946260 | 5381 | | } else if (t.ContainsProxy(proxy)) { |
| | 0 | 5382 | | if (u is TypeProxy) { |
| | 0 | 5383 | | u = GetBaseTypeFromProxy((TypeProxy)u, determinedProxies); |
| | 0 | 5384 | | if (u == null) { |
| | 0 | 5385 | | return null; |
| | | 5386 | | } |
| | 0 | 5387 | | } |
| | 0 | 5388 | | if (Type.SameHead(t, u)) { |
| | 0 | 5389 | | Contract.Assert(t.TypeArgs.Count == u.TypeArgs.Count); |
| | 0 | 5390 | | for (int i = 0; i < t.TypeArgs.Count; i++) { |
| | 0 | 5391 | | var r = GetBaseTypeFromProxy_Eq(proxy, t.TypeArgs[i], u.TypeArgs[i], determinedProxies); |
| | 0 | 5392 | | if (r != null) { |
| | 0 | 5393 | | return r; |
| | | 5394 | | } |
| | 0 | 5395 | | } |
| | 0 | 5396 | | } |
| | 0 | 5397 | | } |
| | 27946260 | 5398 | | return null; |
| | 27946380 | 5399 | | } |
| | | 5400 | | |
| | 21480 | 5401 | | private void GetRelatedTypeProxies(Type t, ISet<TypeProxy> proxies) { |
| | | 5402 | | Contract.Requires(t != null); |
| | | 5403 | | Contract.Requires(proxies != null); |
| | 21480 | 5404 | | var proxy = t.Normalize() as TypeProxy; |
| | 21700 | 5405 | | if (proxy == null || proxies.Contains(proxy)) { |
| | 220 | 5406 | | return; |
| | | 5407 | | } |
| | 21260 | 5408 | | if (Options.Get(CommonOptionBag.TypeInferenceDebug)) { |
| | 0 | 5409 | | Options.OutputWriter.WriteLine("DEBUG: GetRelatedTypeProxies: finding {0} interesting", proxy); |
| | 0 | 5410 | | } |
| | 21260 | 5411 | | proxies.Add(proxy); |
| | | 5412 | | // close over interesting constraints |
| | 34523730 | 5413 | | foreach (var c in AllTypeConstraints) { |
| | 11486650 | 5414 | | var super = c.Super.Normalize(); |
| | 14593690 | 5415 | | if (super.TypeArgs.Exists(ta => ta.Normalize() == proxy)) { |
| | 50 | 5416 | | GetRelatedTypeProxies(c.Sub, proxies); |
| | 50 | 5417 | | } |
| | 11486650 | 5418 | | } |
| | 55996380 | 5419 | | foreach (var xc in AllXConstraints) { |
| | 18644200 | 5420 | | var xc0 = xc.Types[0].Normalize(); |
| | 18817440 | 5421 | | if (xc.ConstraintName == "Assignable" && (xc0 == proxy || xc0.TypeArgs.Exists(ta => ta.Normalize() == proxy))) { |
| | 830 | 5422 | | GetRelatedTypeProxies(xc.Types[1], proxies); |
| | 18644200 | 5423 | | } else if (xc.ConstraintName == "Innable" && xc.Types[1].Normalize() == proxy) { |
| | 0 | 5424 | | GetRelatedTypeProxies(xc.Types[0], proxies); |
| | 18643370 | 5425 | | } else if ((xc.ConstraintName == "ModifiesFrame" || xc.ConstraintName == "ReadsFrame") && xc.Types[1].Normalize( |
| | 0 | 5426 | | GetRelatedTypeProxies(xc.Types[0], proxies); |
| | 0 | 5427 | | } |
| | 18644200 | 5428 | | } |
| | 21480 | 5429 | | } |
| | | 5430 | | |
| | | 5431 | | /// <summary> |
| | | 5432 | | /// Attempts to compute the join of "join", "t", and all of "t"'s known subtype( constraint)s. The join |
| | | 5433 | | /// ignores type parameters. It is assumed that "join" on entry already includes the join of all proxies |
| | | 5434 | | /// in "visited". The empty join is represented by "null". |
| | | 5435 | | /// The return is "true" if the join exists. |
| | | 5436 | | /// </summary> |
| | 50850 | 5437 | | bool JoinOfAllSubtypes(Type t, ref Type joinType, ISet<TypeProxy> visited) { |
| | | 5438 | | Contract.Requires(t != null); |
| | | 5439 | | Contract.Requires(visited != null); |
| | | 5440 | | |
| | 50850 | 5441 | | t = t.NormalizeExpandKeepConstraints(); |
| | | 5442 | | |
| | 50850 | 5443 | | var proxy = t as TypeProxy; |
| | 93810 | 5444 | | if (proxy != null) { |
| | 46460 | 5445 | | if (visited.Contains(proxy)) { |
| | 3500 | 5446 | | return true; |
| | | 5447 | | } |
| | 39460 | 5448 | | visited.Add(proxy); |
| | | 5449 | | |
| | 133230 | 5450 | | foreach (var c in proxy.SubtypeConstraints) { |
| | 4950 | 5451 | | var s = c.Sub.NormalizeExpandKeepConstraints(); |
| | 4950 | 5452 | | if (!JoinOfAllSubtypes(s, ref joinType, visited)) { |
| | 0 | 5453 | | return false; |
| | | 5454 | | } |
| | 4950 | 5455 | | } |
| | 76860 | 5456 | | if (joinType == null) { |
| | | 5457 | | // also consider "Assignable" constraints |
| | 88078440 | 5458 | | foreach (var c in AllXConstraints) { |
| | 29337650 | 5459 | | if (c.ConstraintName == "Assignable" && c.Types[0].Normalize() == proxy) { |
| | 15570 | 5460 | | var s = c.Types[1].NormalizeExpandKeepConstraints(); |
| | 15570 | 5461 | | if (!JoinOfAllSubtypes(s, ref joinType, visited)) { |
| | 0 | 5462 | | return false; |
| | | 5463 | | } |
| | 15570 | 5464 | | } |
| | 29322080 | 5465 | | } |
| | 37400 | 5466 | | } |
| | 39460 | 5467 | | return true; |
| | | 5468 | | } |
| | | 5469 | | |
| | 15700 | 5470 | | if (joinType == null) { |
| | | 5471 | | // stick with what we've got |
| | 7810 | 5472 | | joinType = t; |
| | 7810 | 5473 | | return true; |
| | 160 | 5474 | | } else if (Type.IsHeadSupertypeOf(joinType, t)) { |
| | | 5475 | | // stick with what we've got |
| | 80 | 5476 | | return true; |
| | 0 | 5477 | | } else if (Type.IsHeadSupertypeOf(t, joinType)) { |
| | 0 | 5478 | | joinType = Type.HeadWithProxyArgs(t); |
| | 0 | 5479 | | return true; |
| | 0 | 5480 | | } else { |
| | 0 | 5481 | | joinType = Type.Join(joinType, Type.HeadWithProxyArgs(t), builtIns); // the only way this can succeed is if we |
| | 0 | 5482 | | Contract.Assert(joinType == null || |
| | 0 | 5483 | | joinType.IsObjectQ || joinType.IsObject || |
| | 0 | 5484 | | (joinType is UserDefinedType udt && (udt.ResolvedClass is TraitDecl || (udt.ResolvedClass is Non |
| | 0 | 5485 | | return joinType != null; |
| | | 5486 | | } |
| | 50850 | 5487 | | } |
| | | 5488 | | |
| | | 5489 | | /// <summary> |
| | | 5490 | | /// Attempts to compute the meet of "meet", all of "t"'s known supertype( constraint)s, and, if "includeT" |
| | | 5491 | | /// and "t" has no supertype( constraint)s, "t". |
| | | 5492 | | /// The meet ignores type parameters. (Really?? --KRML) |
| | | 5493 | | /// It is assumed that "meet" on entry already includes the meet of all proxies |
| | | 5494 | | /// in "visited". The empty meet is represented by "null". |
| | | 5495 | | /// The return is "true" if the meet exists. |
| | | 5496 | | /// </summary> |
| | 25530 | 5497 | | bool MeetOfAllSupertypes(Type t, ref Type meet, ISet<TypeProxy> visited, bool includeT) { |
| | | 5498 | | Contract.Requires(t != null); |
| | | 5499 | | Contract.Requires(visited != null); |
| | | 5500 | | |
| | 25530 | 5501 | | t = t.NormalizeExpandKeepConstraints(); |
| | 25530 | 5502 | | var proxy = t as TypeProxy; |
| | 49900 | 5503 | | if (proxy != null) { |
| | 25060 | 5504 | | if (visited.Contains(proxy)) { |
| | 690 | 5505 | | return true; |
| | | 5506 | | } |
| | 23680 | 5507 | | visited.Add(proxy); |
| | | 5508 | | |
| | 23680 | 5509 | | var delegatedToOthers = false; |
| | 78250 | 5510 | | foreach (var c in proxy.SupertypeConstraints) { |
| | 2470 | 5511 | | var s = c.Super.NormalizeExpandKeepConstraints(); |
| | 2470 | 5512 | | delegatedToOthers = true; |
| | 2670 | 5513 | | if (!MeetOfAllSupertypes(s, ref meet, visited, true)) { |
| | 200 | 5514 | | return false; |
| | | 5515 | | } |
| | 2270 | 5516 | | } |
| | 45420 | 5517 | | if (!delegatedToOthers) { |
| | | 5518 | | // also consider "Assignable" constraints |
| | 57482280 | 5519 | | foreach (var c in AllXConstraints) { |
| | 19139400 | 5520 | | if (c.ConstraintName == "Assignable" && c.Types[1].Normalize() == proxy) { |
| | 540 | 5521 | | var s = c.Types[0].NormalizeExpandKeepConstraints(); |
| | 540 | 5522 | | delegatedToOthers = true; |
| | 660 | 5523 | | if (!MeetOfAllSupertypes(s, ref meet, visited, true)) { |
| | 120 | 5524 | | return false; |
| | | 5525 | | } |
| | 420 | 5526 | | } |
| | 19138740 | 5527 | | } |
| | 21820 | 5528 | | } |
| | 25200 | 5529 | | if (delegatedToOthers) { |
| | 1840 | 5530 | | return true; |
| | 42360 | 5531 | | } else if (!includeT) { |
| | 20840 | 5532 | | return true; |
| | 1220 | 5533 | | } else if (meet == null || meet.Normalize() == proxy) { |
| | 540 | 5534 | | meet = proxy; |
| | 540 | 5535 | | return true; |
| | 140 | 5536 | | } else { |
| | 140 | 5537 | | return false; |
| | | 5538 | | } |
| | | 5539 | | } |
| | | 5540 | | |
| | 2240 | 5541 | | if (meet == null) { |
| | 1080 | 5542 | | meet = Type.HeadWithProxyArgs(t); |
| | 1080 | 5543 | | return true; |
| | 110 | 5544 | | } else if (Type.IsHeadSupertypeOf(t, meet)) { |
| | | 5545 | | // stick with what we've got |
| | 30 | 5546 | | return true; |
| | 50 | 5547 | | } else if (Type.IsHeadSupertypeOf(meet, t)) { |
| | 0 | 5548 | | meet = Type.HeadWithProxyArgs(t); |
| | 0 | 5549 | | return true; |
| | 50 | 5550 | | } else { |
| | 50 | 5551 | | meet = Type.Meet(meet, Type.HeadWithProxyArgs(t), builtIns); |
| | 50 | 5552 | | return meet != null; |
| | | 5553 | | } |
| | 25530 | 5554 | | } |
| | | 5555 | | |
| | | 5556 | | /// <summary> |
| | | 5557 | | /// Check that the type uses formal type parameters in a way that is agreeable with their variance specifications. |
| | | 5558 | | /// "context == Co" says that "type" is allowed to vary in the positive direction. |
| | | 5559 | | /// "context == Contra" says that "type" is allowed to vary in the negative direction. |
| | | 5560 | | /// "context == Non" says that "type" must not vary at all. |
| | | 5561 | | /// * "lax" says that the context is not strict -- type parameters declared to be strict must not be used in a lax c |
| | | 5562 | | /// </summary> |
| | 1260 | 5563 | | public void CheckVariance(Type type, ICallable enclosingTypeDefinition, TypeParameter.TPVariance context, bool lax) |
| | | 5564 | | Contract.Requires(type != null); |
| | | 5565 | | Contract.Requires(enclosingTypeDefinition != null); |
| | | 5566 | | |
| | 1260 | 5567 | | type = type.Normalize(); // we keep constraints, since subset types have their own type-parameter variance specif |
| | 1260 | 5568 | | if (type is BasicType) { |
| | | 5569 | | // fine |
| | 1260 | 5570 | | } else if (type is MapType) { |
| | 0 | 5571 | | var t = (MapType)type; |
| | | 5572 | | // If its an infinite map, the domain's context is lax |
| | 0 | 5573 | | CheckVariance(t.Domain, enclosingTypeDefinition, context, lax || !t.Finite); |
| | 0 | 5574 | | CheckVariance(t.Range, enclosingTypeDefinition, context, lax); |
| | 1260 | 5575 | | } else if (type is SetType) { |
| | 0 | 5576 | | var t = (SetType)type; |
| | | 5577 | | // If its an infinite set, the argument's context is lax |
| | 0 | 5578 | | CheckVariance(t.Arg, enclosingTypeDefinition, context, lax || !t.Finite); |
| | 1260 | 5579 | | } else if (type is CollectionType) { |
| | 0 | 5580 | | var t = (CollectionType)type; |
| | 0 | 5581 | | CheckVariance(t.Arg, enclosingTypeDefinition, context, lax); |
| | 2520 | 5582 | | } else if (type is UserDefinedType) { |
| | 1260 | 5583 | | var t = (UserDefinedType)type; |
| | 1680 | 5584 | | if (t.ResolvedClass is TypeParameter tp) { |
| | 420 | 5585 | | if (tp.Variance != TypeParameter.TPVariance.Non && tp.Variance != context) { |
| | 0 | 5586 | | reporter.Error(MessageSource.Resolver, t.tok, "formal type parameter '{0}' is not used according to its vari |
| | 420 | 5587 | | } else if (tp.StrictVariance && lax) { |
| | | 5588 | | string hint; |
| | 0 | 5589 | | if (tp.VarianceSyntax == TypeParameter.TPVarianceSyntax.NonVariant_Strict) { |
| | 0 | 5590 | | hint = string.Format(" (perhaps try declaring '{0}' as '-{0}' or '!{0}')", tp.Name); |
| | 0 | 5591 | | } else { |
| | 0 | 5592 | | Contract.Assert(tp.VarianceSyntax == TypeParameter.TPVarianceSyntax.Covariant_Strict); |
| | 0 | 5593 | | hint = string.Format(" (perhaps try changing the declaration from '+{0}' to '*{0}')", tp.Name); |
| | 0 | 5594 | | } |
| | 0 | 5595 | | reporter.Error(MessageSource.Resolver, t.tok, "formal type parameter '{0}' is not used according to its vari |
| | 0 | 5596 | | } |
| | 1260 | 5597 | | } else { |
| | 840 | 5598 | | var resolvedClass = t.ResolvedClass; |
| | 840 | 5599 | | Contract.Assert(resolvedClass != null); // follows from that the given type was successfully resolved |
| | 840 | 5600 | | Contract.Assert(resolvedClass.TypeArgs.Count == t.TypeArgs.Count); |
| | 840 | 5601 | | if (lax) { |
| | | 5602 | | // we have to be careful about uses of the type being defined |
| | 0 | 5603 | | var cg = enclosingTypeDefinition.EnclosingModule.CallGraph; |
| | 0 | 5604 | | var t0 = resolvedClass as ICallable; |
| | 0 | 5605 | | if (t0 != null && cg.GetSCCRepresentative(t0) == cg.GetSCCRepresentative(enclosingTypeDefinition)) { |
| | 0 | 5606 | | reporter.Error(MessageSource.Resolver, t.tok, "using the type being defined ('{0}') here would cause a log |
| | 0 | 5607 | | } |
| | 0 | 5608 | | } |
| | 2940 | 5609 | | for (int i = 0; i < t.TypeArgs.Count; i++) { |
| | 420 | 5610 | | Type p = t.TypeArgs[i]; |
| | 420 | 5611 | | var tpFormal = resolvedClass.TypeArgs[i]; |
| | 420 | 5612 | | CheckVariance(p, enclosingTypeDefinition, |
| | 420 | 5613 | | context == TypeParameter.TPVariance.Non ? context : |
| | 420 | 5614 | | context == TypeParameter.TPVariance.Co ? tpFormal.Variance : |
| | 420 | 5615 | | TypeParameter.Negate(tpFormal.Variance), |
| | 420 | 5616 | | lax || !tpFormal.StrictVariance); |
| | 420 | 5617 | | } |
| | 840 | 5618 | | } |
| | 1260 | 5619 | | } else { |
| | 0 | 5620 | | Contract.Assert(false); throw new cce.UnreachableException(); // unexpected type |
| | | 5621 | | } |
| | 1260 | 5622 | | } |
| | | 5623 | | |
| | | 5624 | | /// <summary> |
| | | 5625 | | /// See ConstrainToIntegerType description for the overload above. |
| | | 5626 | | /// </summary> |
| | 18900 | 5627 | | void ConstrainToIntegerType(IToken tok, Type type, bool allowBitVector, TypeConstraint.ErrorMsg errorMsg) { |
| | | 5628 | | Contract.Requires(tok != null); |
| | | 5629 | | Contract.Requires(type != null); |
| | | 5630 | | Contract.Requires(errorMsg != null); |
| | | 5631 | | // We do two constraints: the first can aid in determining types, but allows bit-vectors; the second excludes bit- |
| | | 5632 | | // However, we reuse the error message, so that only one error gets reported. |
| | 18900 | 5633 | | ConstrainSubtypeRelation(new IntVarietiesSupertype(), type, errorMsg); |
| | 19320 | 5634 | | if (!allowBitVector) { |
| | 420 | 5635 | | AddXConstraint(tok, "IntegerType", type, errorMsg); |
| | 420 | 5636 | | } |
| | 18900 | 5637 | | } |
| | | 5638 | | |
| | | 5639 | | /// <summary> |
| | | 5640 | | /// Attempts to rewrite a datatype update into more primitive operations, after doing the appropriate resolution che |
| | | 5641 | | /// Upon success, returns that rewritten expression and sets "legalSourceConstructors". |
| | | 5642 | | /// Upon some resolution error, return null. |
| | | 5643 | | /// |
| | | 5644 | | /// Actually, the method returns two expressions (or returns "(null, null)"). The first expression is the desugaring |
| | | 5645 | | /// used when the DatatypeUpdateExpr is used in a ghost context. The second is to be used for a compiled context. In |
| | | 5646 | | /// case, "legalSourceConstructors" contains both ghost and compiled constructors. |
| | | 5647 | | /// |
| | | 5648 | | /// The reason for computing both desugarings here is that it's too early to tell if the DatatypeUpdateExpr is being |
| | | 5649 | | /// a ghost or compiled context. This is a consequence of doing the deguaring so early. But it's also convenient to |
| | | 5650 | | /// desugaring during resolution, because then the desugaring can be constructed as a non-resolved expression on whi |
| | | 5651 | | /// is called--this is easier than constructing an already-resolved expression. |
| | | 5652 | | /// </summary> |
| | | 5653 | | (Expression, Expression) ResolveDatatypeUpdate(IToken tok, Expression root, DatatypeDecl dt, List<Tuple<IToken, stri |
| | 0 | 5654 | | ResolutionContext resolutionContext, out List<MemberDecl> members, out List<DatatypeCtor> legalSourceConstructors) |
| | | 5655 | | Contract.Requires(tok != null); |
| | | 5656 | | Contract.Requires(root != null); |
| | | 5657 | | Contract.Requires(dt != null); |
| | | 5658 | | Contract.Requires(memberUpdates != null); |
| | | 5659 | | Contract.Requires(resolutionContext != null); |
| | | 5660 | | |
| | 0 | 5661 | | legalSourceConstructors = null; |
| | 0 | 5662 | | members = new List<MemberDecl>(); |
| | | 5663 | | |
| | | 5664 | | // First, compute the list of candidate result constructors, that is, the constructors |
| | | 5665 | | // that have all of the mentioned destructors. Issue errors for duplicated names and for |
| | | 5666 | | // names that are not destructors in the datatype. |
| | 0 | 5667 | | var candidateResultCtors = dt.Ctors; // list of constructors that have all the so-far-mentioned destructors |
| | 0 | 5668 | | var memberNames = new HashSet<string>(); |
| | 0 | 5669 | | var rhsBindings = new Dictionary<string, Tuple<BoundVar/*let variable*/, IdentifierExpr/*id expr for let variable* |
| | 0 | 5670 | | var subst = TypeParameter.SubstitutionMap(dt.TypeArgs, root.Type.NormalizeExpand().TypeArgs); |
| | 0 | 5671 | | foreach (var entry in memberUpdates) { |
| | 0 | 5672 | | var destructor_str = entry.Item2; |
| | 0 | 5673 | | if (memberNames.Contains(destructor_str)) { |
| | 0 | 5674 | | reporter.Error(MessageSource.Resolver, entry.Item1, "duplicate update member '{0}'", destructor_str); |
| | 0 | 5675 | | } else { |
| | 0 | 5676 | | memberNames.Add(destructor_str); |
| | 0 | 5677 | | if (!classMembers[dt].TryGetValue(destructor_str, out var member)) { |
| | 0 | 5678 | | reporter.Error(MessageSource.Resolver, entry.Item1, "member '{0}' does not exist in datatype '{1}'", destruc |
| | 0 | 5679 | | } else if (!(member is DatatypeDestructor)) { |
| | 0 | 5680 | | reporter.Error(MessageSource.Resolver, entry.Item1, "member '{0}' is not a destructor in datatype '{1}'", de |
| | 0 | 5681 | | } else { |
| | 0 | 5682 | | members.Add(member); |
| | 0 | 5683 | | var destructor = (DatatypeDestructor)member; |
| | 0 | 5684 | | var intersection = new List<DatatypeCtor>(candidateResultCtors.Intersect(destructor.EnclosingCtors)); |
| | 0 | 5685 | | if (intersection.Count == 0) { |
| | 0 | 5686 | | reporter.Error(MessageSource.Resolver, entry.Item1, |
| | 0 | 5687 | | "updated datatype members must belong to the same constructor (unlike the previously mentioned destructo |
| | 0 | 5688 | | destructor_str, DatatypeDestructor.PrintableCtorNameList(candidateResultCtors, "or")); |
| | 0 | 5689 | | } else { |
| | 0 | 5690 | | candidateResultCtors = intersection; |
| | 0 | 5691 | | if (destructor.IsGhost) { |
| | 0 | 5692 | | rhsBindings.Add(destructor_str, new Tuple<BoundVar, IdentifierExpr, Expression>(null, null, entry.Item3) |
| | 0 | 5693 | | } else { |
| | 0 | 5694 | | var xName = FreshTempVarName(string.Format("dt_update#{0}#", destructor_str), resolutionContext.CodeCont |
| | 0 | 5695 | | var xVar = new BoundVar(new AutoGeneratedToken(tok), xName, destructor.Type.Subst(subst)); |
| | 0 | 5696 | | var x = new IdentifierExpr(new AutoGeneratedToken(tok), xVar); |
| | 0 | 5697 | | rhsBindings.Add(destructor_str, new Tuple<BoundVar, IdentifierExpr, Expression>(xVar, x, entry.Item3)); |
| | 0 | 5698 | | } |
| | 0 | 5699 | | } |
| | 0 | 5700 | | } |
| | 0 | 5701 | | } |
| | 0 | 5702 | | } |
| | 0 | 5703 | | if (candidateResultCtors.Count == 0) { |
| | 0 | 5704 | | return (null, null); |
| | | 5705 | | } |
| | | 5706 | | |
| | | 5707 | | // Check that every candidate result constructor has given a name to all of its parameters. |
| | 0 | 5708 | | var hasError = false; |
| | 0 | 5709 | | foreach (var ctor in candidateResultCtors) { |
| | 0 | 5710 | | if (ctor.Formals.Exists(f => !f.HasName)) { |
| | 0 | 5711 | | reporter.Error(MessageSource.Resolver, tok, |
| | 0 | 5712 | | "candidate result constructor '{0}' has an anonymous parameter (to use in datatype update expression, name a |
| | 0 | 5713 | | ctor.Name); |
| | 0 | 5714 | | hasError = true; |
| | 0 | 5715 | | } |
| | 0 | 5716 | | } |
| | 0 | 5717 | | if (hasError) { |
| | 0 | 5718 | | return (null, null); |
| | | 5719 | | } |
| | | 5720 | | |
| | | 5721 | | // The legal source constructors are the candidate result constructors. (Yep, two names for the same thing.) |
| | 0 | 5722 | | legalSourceConstructors = candidateResultCtors; |
| | 0 | 5723 | | Contract.Assert(1 <= legalSourceConstructors.Count); |
| | | 5724 | | |
| | 0 | 5725 | | var desugaringForGhostContext = DesugarDatatypeUpdate(tok, root, dt, candidateResultCtors, rhsBindings, resolution |
| | 0 | 5726 | | var nonGhostConstructors = candidateResultCtors.Where(ctor => !ctor.IsGhost).ToList(); |
| | 0 | 5727 | | if (nonGhostConstructors.Count == candidateResultCtors.Count) { |
| | 0 | 5728 | | return (desugaringForGhostContext, desugaringForGhostContext); |
| | | 5729 | | } |
| | 0 | 5730 | | var desugaringForCompiledContext = DesugarDatatypeUpdate(tok, root, dt, nonGhostConstructors, rhsBindings, resolut |
| | 0 | 5731 | | return (desugaringForGhostContext, desugaringForCompiledContext); |
| | 0 | 5732 | | } |
| | | 5733 | | |
| | | 5734 | | /// <summary> |
| | | 5735 | | // Rewrite the datatype update root.(x := X, y := Y, ...) to: |
| | | 5736 | | /// var d := root; |
| | | 5737 | | /// var x := X; // EXCEPT: don't do this for ghost fields |
| | | 5738 | | /// var y := Y; |
| | | 5739 | | /// .. |
| | | 5740 | | /// if d.CandidateResultConstructor0 then |
| | | 5741 | | /// CandidateResultConstructor0(x, y, ..., d.f0, d.f1, ...) // for a ghost field x, use the expression X dire |
| | | 5742 | | /// else if d.CandidateResultConstructor1 then |
| | | 5743 | | /// CandidateResultConstructor0(x, y, ..., d.g0, d.g1, ...) |
| | | 5744 | | /// ... |
| | | 5745 | | /// else |
| | | 5746 | | /// CandidateResultConstructorN(x, y, ..., d.k0, d.k1, ...) |
| | | 5747 | | /// </summary> |
| | | 5748 | | private Expression DesugarDatatypeUpdate(IToken tok, Expression root, DatatypeDecl dt, List<DatatypeCtor> candidateR |
| | 0 | 5749 | | Dictionary<string, Tuple<BoundVar, IdentifierExpr, Expression>> rhsBindings, ResolutionContext resolutionContext) |
| | | 5750 | | |
| | 0 | 5751 | | if (candidateResultCtors.Count == 0) { |
| | 0 | 5752 | | return root; |
| | | 5753 | | } |
| | 0 | 5754 | | Expression rewrite = null; |
| | | 5755 | | // Create a unique name for d', the variable we introduce in the let expression |
| | 0 | 5756 | | var dName = FreshTempVarName("dt_update_tmp#", resolutionContext.CodeContext); |
| | 0 | 5757 | | var dVar = new BoundVar(new AutoGeneratedToken(tok), dName, root.Type); |
| | 0 | 5758 | | var d = new IdentifierExpr(new AutoGeneratedToken(tok), dVar); |
| | 0 | 5759 | | Expression body = null; |
| | 0 | 5760 | | candidateResultCtors.Reverse(); |
| | 0 | 5761 | | foreach (var crc in candidateResultCtors) { |
| | | 5762 | | // Build the arguments to the datatype constructor, using the updated value in the appropriate slot |
| | 0 | 5763 | | var ctorArguments = new List<Expression>(); |
| | 0 | 5764 | | var actualBindings = new List<ActualBinding>(); |
| | 0 | 5765 | | foreach (var f in crc.Formals) { |
| | | 5766 | | Expression ctorArg; |
| | 0 | 5767 | | if (rhsBindings.TryGetValue(f.Name, out var info)) { |
| | 0 | 5768 | | ctorArg = info.Item2 ?? info.Item3; |
| | 0 | 5769 | | } else { |
| | 0 | 5770 | | ctorArg = new ExprDotName(tok, d, f.Name, null); |
| | 0 | 5771 | | } |
| | 0 | 5772 | | ctorArguments.Add(ctorArg); |
| | 0 | 5773 | | var bindingName = new Token(tok.line, tok.col) { |
| | 0 | 5774 | | Uri = tok.Uri, |
| | 0 | 5775 | | val = f.Name |
| | 0 | 5776 | | }; |
| | 0 | 5777 | | actualBindings.Add(new ActualBinding(bindingName, ctorArg)); |
| | 0 | 5778 | | } |
| | 0 | 5779 | | var ctor_call = new DatatypeValue(tok, crc.EnclosingDatatype.Name, crc.Name, actualBindings); |
| | | 5780 | | // in the following line, resolve to root.Type, so that type parameters get filled in appropriately |
| | 0 | 5781 | | ResolveDatatypeValue(resolutionContext, ctor_call, dt, root.Type.NormalizeExpand()); |
| | | 5782 | | |
| | 0 | 5783 | | if (body == null) { |
| | 0 | 5784 | | body = ctor_call; |
| | 0 | 5785 | | } else { |
| | | 5786 | | // body = if d.crc? then ctor_call else body |
| | 0 | 5787 | | var guard = new ExprDotName(tok, d, crc.QueryField.Name, null); |
| | 0 | 5788 | | body = new ITEExpr(tok, false, guard, ctor_call, body); |
| | 0 | 5789 | | } |
| | 0 | 5790 | | } |
| | 0 | 5791 | | Contract.Assert(body != null); // because there was at least one element in candidateResultCtors |
| | | 5792 | | |
| | | 5793 | | // Wrap the let's around body |
| | 0 | 5794 | | rewrite = body; |
| | 0 | 5795 | | foreach (var entry in rhsBindings) { |
| | 0 | 5796 | | if (entry.Value.Item1 != null) { |
| | 0 | 5797 | | var lhs = new CasePattern<BoundVar>(tok, entry.Value.Item1); |
| | 0 | 5798 | | rewrite = new LetExpr(tok, new List<CasePattern<BoundVar>>() { lhs }, new List<Expression>() { entry.Value.Ite |
| | 0 | 5799 | | } |
| | 0 | 5800 | | } |
| | 0 | 5801 | | var dVarPat = new CasePattern<BoundVar>(tok, dVar); |
| | 0 | 5802 | | rewrite = new LetExpr(tok, new List<CasePattern<BoundVar>>() { dVarPat }, new List<Expression>() { root }, rewrite |
| | 0 | 5803 | | Contract.Assert(rewrite != null); |
| | 0 | 5804 | | ResolveExpression(rewrite, resolutionContext); |
| | 0 | 5805 | | return rewrite; |
| | 0 | 5806 | | } |
| | | 5807 | | |
| | | 5808 | | public Expression ResolveNameSegment(NameSegment expr, bool isLastNameSegment, List<ActualBinding> args, |
| | 700210 | 5809 | | ResolutionContext resolutionContext, bool allowMethodCall, bool complain = true) { |
| | 700210 | 5810 | | return ResolveNameSegment(expr, isLastNameSegment, args, resolutionContext, allowMethodCall, complain, out _); |
| | 700210 | 5811 | | } |
| | | 5812 | | |
| | | 5813 | | /// <summary> |
| | | 5814 | | /// Look up expr.Name in the following order: |
| | | 5815 | | /// 0. Local variable, parameter, or bound variable. |
| | | 5816 | | /// (Language design note: If this clashes with something of interest, one can always rename the local variable |
| | | 5817 | | /// 1. Member of enclosing class (an implicit "this" is inserted, if needed) |
| | | 5818 | | /// 2. If isLastNameSegment: |
| | | 5819 | | /// Unambiguous constructor name of a datatype in the enclosing module (if two constructors have the same name, |
| | | 5820 | | /// (Language design note: If the constructor name is ambiguous or if one of the steps above takes priority, on |
| | | 5821 | | /// 3. Member of the enclosing module (type name or the name of a module) |
| | | 5822 | | /// 4. Static function or method in the enclosing module or its imports |
| | | 5823 | | /// 5. If !isLastNameSegment: |
| | | 5824 | | /// Unambiguous constructor name of a datatype in the enclosing module |
| | | 5825 | | /// |
| | | 5826 | | /// </summary> |
| | | 5827 | | /// <param name="expr"></param> |
| | | 5828 | | /// <param name="isLastNameSegment">Indicates that the NameSegment is not directly enclosed in another NameSegment o |
| | | 5829 | | /// <param name="args">If the NameSegment is enclosed in an ApplySuffix, then these are the arguments. The method r |
| | | 5830 | | /// that these arguments, if any, were not used. If args is non-null and the method does use them, the method retur |
| | | 5831 | | /// that incorporates these arguments.</param> |
| | | 5832 | | /// <param name="resolutionContext"></param> |
| | | 5833 | | /// <param name="allowMethodCall">If false, generates an error if the name denotes a method. If true and the name de |
| | | 5834 | | /// a MemberSelectExpr whose .Member is a Method.</param> |
| | | 5835 | | /// <param name="shadowedModule">If the name being resolved shadows an imported module, then that module is reported |
| | | 5836 | | /// through this parameter. This happens when module <c>Option</c> in <c>import opened Option</c> also contains a |
| | | 5837 | | /// <c>datatype Option</c>, in which case <c>Option</c> refers to the datatype, not the module |
| | | 5838 | | /// (https://github.com/dafny-lang/dafny/issues/1996).</param> |
| | 704830 | 5839 | | Expression ResolveNameSegment(NameSegment expr, bool isLastNameSegment, List<ActualBinding> args, ResolutionContext |
| | | 5840 | | Contract.Requires(expr != null); |
| | | 5841 | | Contract.Requires(!expr.WasResolved()); |
| | | 5842 | | Contract.Requires(resolutionContext != null); |
| | | 5843 | | Contract.Ensures(Contract.Result<Expression>() == null || args != null); |
| | | 5844 | | |
| | 704830 | 5845 | | shadowedModule = null; |
| | | 5846 | | |
| | 704830 | 5847 | | if (expr.OptTypeArguments != null) { |
| | 0 | 5848 | | foreach (var ty in expr.OptTypeArguments) { |
| | 0 | 5849 | | ResolveType(expr.tok, ty, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 5850 | | } |
| | 0 | 5851 | | } |
| | | 5852 | | |
| | 704830 | 5853 | | Expression r = null; // the resolved expression, if successful |
| | 704830 | 5854 | | Expression rWithArgs = null; // the resolved expression after incorporating "args" |
| | | 5855 | | |
| | | 5856 | | // For 0: |
| | | 5857 | | IVariable v; |
| | | 5858 | | // For 1: |
| | | 5859 | | // For 1 and 4: |
| | 704830 | 5860 | | MemberDecl member = null; |
| | | 5861 | | // For 2 and 5: |
| | | 5862 | | // For 3: |
| | | 5863 | | |
| | 704830 | 5864 | | var name = resolutionContext.InReveal ? "reveal_" + expr.Name : expr.Name; |
| | 704830 | 5865 | | v = scope.Find(name); |
| | 1390570 | 5866 | | if (v != null) { |
| | | 5867 | | // ----- 0. local variable, parameter, or bound variable |
| | 685740 | 5868 | | if (expr.OptTypeArguments != null) { |
| | 0 | 5869 | | if (complain) { |
| | 0 | 5870 | | reporter.Error(MessageSource.Resolver, expr.tok, "variable '{0}' does not take any type parameters", name); |
| | 0 | 5871 | | } else { |
| | 0 | 5872 | | expr.ResolvedExpression = null; |
| | 0 | 5873 | | return null; |
| | | 5874 | | } |
| | 0 | 5875 | | } |
| | 685740 | 5876 | | r = new IdentifierExpr(expr.tok, v); |
| | 723920 | 5877 | | } else if (currentClass is TopLevelDeclWithMembers cl && classMembers.TryGetValue(cl, out var members) && members. |
| | | 5878 | | // ----- 1. member of the enclosing class |
| | | 5879 | | Expression receiver; |
| | 38180 | 5880 | | if (member.IsStatic) { |
| | 19090 | 5881 | | receiver = new StaticReceiverExpr(expr.tok, UserDefinedType.FromTopLevelDecl(expr.tok, currentClass, currentCl |
| | 19090 | 5882 | | } else { |
| | 0 | 5883 | | if (!scope.AllowInstance) { |
| | 0 | 5884 | | if (complain) { |
| | 0 | 5885 | | reporter.Error(MessageSource.Resolver, expr.tok, "'this' is not allowed in a 'static' context"); //TODO: R |
| | 0 | 5886 | | } else { |
| | 0 | 5887 | | expr.ResolvedExpression = null; |
| | 0 | 5888 | | return null; |
| | | 5889 | | } |
| | | 5890 | | // nevertheless, set "receiver" to a value so we can continue resolution |
| | 0 | 5891 | | } |
| | 0 | 5892 | | receiver = new ImplicitThisExpr(expr.tok); |
| | 0 | 5893 | | receiver.Type = GetThisType(expr.tok, currentClass); // resolve here |
| | 0 | 5894 | | } |
| | 19090 | 5895 | | r = ResolveExprDotCall(expr.tok, receiver, null, member, args, expr.OptTypeArguments, resolutionContext, allowMe |
| | 19090 | 5896 | | } else if (isLastNameSegment && moduleInfo.Ctors.TryGetValue(name, out var pair)) { |
| | | 5897 | | // ----- 2. datatype constructor |
| | 0 | 5898 | | if (ResolveDatatypeConstructor(expr, args, resolutionContext, complain, pair, name, ref r, ref rWithArgs)) { |
| | 0 | 5899 | | return null; |
| | | 5900 | | } |
| | 0 | 5901 | | } else if (moduleInfo.TopLevels.TryGetValue(name, out var decl)) { |
| | | 5902 | | // ----- 3. Member of the enclosing module |
| | | 5903 | | |
| | | 5904 | | // Record which imported module, if any, was shadowed by `name` in the current module. |
| | 0 | 5905 | | shadowedModule = moduleInfo.ShadowedImportedModules.GetValueOrDefault(name); |
| | | 5906 | | |
| | 0 | 5907 | | if (decl is AmbiguousTopLevelDecl) { |
| | 0 | 5908 | | var ad = (AmbiguousTopLevelDecl)decl; |
| | 0 | 5909 | | if (complain) { |
| | 0 | 5910 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a type in one of the mo |
| | 0 | 5911 | | } else { |
| | 0 | 5912 | | expr.ResolvedExpression = null; |
| | 0 | 5913 | | return null; |
| | | 5914 | | } |
| | 0 | 5915 | | } else { |
| | | 5916 | | // We have found a module name or a type name, neither of which is an expression. However, the NameSegment we' |
| | | 5917 | | // looking at may be followed by a further suffix that makes this into an expresion. We postpone the rest of t |
| | | 5918 | | // resolution to any such suffix. For now, we create a temporary expression that will never be seen by the com |
| | | 5919 | | // or verifier, just to have a placeholder where we can recorded what we have found. |
| | 0 | 5920 | | if (!isLastNameSegment) { |
| | 0 | 5921 | | if (decl is ClassDecl cd && cd.NonNullTypeDecl != null && name != cd.NonNullTypeDecl.Name) { |
| | | 5922 | | // A possibly-null type C? was mentioned. But it does not have any further members. The program should hav |
| | | 5923 | | // the name of the class, C. Report an error and continue. |
| | 0 | 5924 | | if (complain) { |
| | 0 | 5925 | | reporter.Error(MessageSource.Resolver, expr.tok, "To access members of {0} '{1}', write '{1}', not '{2}' |
| | 0 | 5926 | | } else { |
| | 0 | 5927 | | expr.ResolvedExpression = null; |
| | 0 | 5928 | | return null; |
| | | 5929 | | } |
| | 0 | 5930 | | } |
| | 0 | 5931 | | } |
| | 0 | 5932 | | r = CreateResolver_IdentifierExpr(expr.tok, name, expr.OptTypeArguments, decl); |
| | 0 | 5933 | | } |
| | | 5934 | | |
| | 0 | 5935 | | } else if (moduleInfo.StaticMembers.TryGetValue(name, out member)) { |
| | | 5936 | | // ----- 4. static member of the enclosing module |
| | 0 | 5937 | | Contract.Assert(member.IsStatic); // moduleInfo.StaticMembers is supposed to contain only static members of the |
| | 0 | 5938 | | if (member is AmbiguousMemberDecl) { |
| | 0 | 5939 | | var ambiguousMember = (AmbiguousMemberDecl)member; |
| | 0 | 5940 | | if (complain) { |
| | 0 | 5941 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a static member in one |
| | 0 | 5942 | | } else { |
| | 0 | 5943 | | expr.ResolvedExpression = null; |
| | 0 | 5944 | | return null; |
| | | 5945 | | } |
| | 0 | 5946 | | } else { |
| | 0 | 5947 | | var receiver = new StaticReceiverExpr(expr.tok, (ClassDecl)member.EnclosingClass, true); |
| | 0 | 5948 | | r = ResolveExprDotCall(expr.tok, receiver, null, member, args, expr.OptTypeArguments, resolutionContext, allow |
| | 0 | 5949 | | } |
| | | 5950 | | |
| | 0 | 5951 | | } else if (!isLastNameSegment && moduleInfo.Ctors.TryGetValue(name, out pair)) { |
| | | 5952 | | // ----- 5. datatype constructor |
| | 0 | 5953 | | if (ResolveDatatypeConstructor(expr, args, resolutionContext, complain, pair, name, ref r, ref rWithArgs)) { |
| | 0 | 5954 | | return null; |
| | | 5955 | | } |
| | | 5956 | | |
| | 0 | 5957 | | } else { |
| | | 5958 | | // ----- None of the above |
| | 0 | 5959 | | if (complain) { |
| | 0 | 5960 | | if (resolutionContext.InReveal) { |
| | 0 | 5961 | | reporter.Error(MessageSource.Resolver, expr.tok, "cannot reveal '{0}' because no constant, assert label, or |
| | 0 | 5962 | | } else { |
| | 0 | 5963 | | reporter.Error(MessageSource.Resolver, expr.tok, "unresolved identifier: {0}", name); |
| | 0 | 5964 | | } |
| | 0 | 5965 | | } else { |
| | 0 | 5966 | | expr.ResolvedExpression = null; |
| | 0 | 5967 | | return null; |
| | | 5968 | | } |
| | 0 | 5969 | | } |
| | | 5970 | | |
| | 704830 | 5971 | | if (r == null) { |
| | | 5972 | | // an error has been reported above; we won't fill in .ResolvedExpression, but we still must fill in .Type |
| | 0 | 5973 | | expr.Type = new InferredTypeProxy(); |
| | 704830 | 5974 | | } else { |
| | 704830 | 5975 | | expr.ResolvedExpression = r; |
| | 704830 | 5976 | | var rt = r.Type; |
| | 704830 | 5977 | | var nt = rt.UseInternalSynonym(); |
| | 704830 | 5978 | | expr.Type = nt; |
| | 704830 | 5979 | | } |
| | 704830 | 5980 | | return rWithArgs; |
| | 704830 | 5981 | | } |
| | | 5982 | | |
| | 0 | 5983 | | private bool ResolveDatatypeConstructor(NameSegment expr, List<ActualBinding>/*?*/ args, ResolutionContext resolutio |
| | | 5984 | | Contract.Requires(expr != null); |
| | | 5985 | | Contract.Requires(resolutionContext != null); |
| | | 5986 | | |
| | 0 | 5987 | | if (pair.Item2) { |
| | | 5988 | | // there is more than one constructor with this name |
| | 0 | 5989 | | if (complain) { |
| | 0 | 5990 | | reporter.Error(MessageSource.Resolver, expr.tok, "the name '{0}' denotes a datatype constructor, but does not |
| | 0 | 5991 | | pair.Item1.EnclosingDatatype.Name); |
| | 0 | 5992 | | } else { |
| | 0 | 5993 | | expr.ResolvedExpression = null; |
| | 0 | 5994 | | return true; |
| | | 5995 | | } |
| | 0 | 5996 | | } else { |
| | 0 | 5997 | | if (expr.OptTypeArguments != null) { |
| | 0 | 5998 | | if (complain) { |
| | 0 | 5999 | | reporter.Error(MessageSource.Resolver, expr.tok, "datatype constructor does not take any type parameters ('{ |
| | 0 | 6000 | | } else { |
| | 0 | 6001 | | expr.ResolvedExpression = null; |
| | 0 | 6002 | | return true; |
| | | 6003 | | } |
| | 0 | 6004 | | } |
| | 0 | 6005 | | var rr = new DatatypeValue(expr.tok, pair.Item1.EnclosingDatatype.Name, name, args ?? new List<ActualBinding>()) |
| | 0 | 6006 | | bool ok = ResolveDatatypeValue(resolutionContext, rr, pair.Item1.EnclosingDatatype, null, complain); |
| | 0 | 6007 | | if (!ok) { |
| | 0 | 6008 | | expr.ResolvedExpression = null; |
| | 0 | 6009 | | return true; |
| | | 6010 | | } |
| | 0 | 6011 | | if (args == null) { |
| | 0 | 6012 | | r = rr; |
| | 0 | 6013 | | } else { |
| | 0 | 6014 | | r = rr; // this doesn't really matter, since we're returning an "rWithArgs" (but if would have been proper to |
| | 0 | 6015 | | rWithArgs = rr; |
| | 0 | 6016 | | } |
| | 0 | 6017 | | } |
| | 0 | 6018 | | return false; |
| | 0 | 6019 | | } |
| | | 6020 | | |
| | | 6021 | | /// <summary> |
| | | 6022 | | /// Look up expr.Name in the following order: |
| | | 6023 | | /// 0. Type parameter |
| | | 6024 | | /// 1. Member of enclosing class (an implicit "this" is inserted, if needed) |
| | | 6025 | | /// 2. Member of the enclosing module (type name or the name of a module) |
| | | 6026 | | /// 3. Static function or method in the enclosing module or its imports |
| | | 6027 | | /// |
| | | 6028 | | /// Note: 1 and 3 are not used now, but they will be of interest when async task types are supported. |
| | | 6029 | | /// </summary> |
| | 111670 | 6030 | | void ResolveNameSegment_Type(NameSegment expr, ResolutionContext resolutionContext, ResolveTypeOption option, List<T |
| | | 6031 | | Contract.Requires(expr != null); |
| | | 6032 | | Contract.Requires(!expr.WasResolved()); |
| | | 6033 | | Contract.Requires(resolutionContext != null); |
| | | 6034 | | Contract.Requires((option.Opt == ResolveTypeOptionEnum.DontInfer || option.Opt == ResolveTypeOptionEnum.InferTypeP |
| | | 6035 | | |
| | 135280 | 6036 | | if (expr.OptTypeArguments != null) { |
| | 248040 | 6037 | | foreach (var ty in expr.OptTypeArguments) { |
| | 59070 | 6038 | | ResolveType(expr.tok, ty, resolutionContext, option, defaultTypeArguments); |
| | 59070 | 6039 | | } |
| | 23610 | 6040 | | } |
| | | 6041 | | |
| | 111670 | 6042 | | Expression r = null; // the resolved expression, if successful |
| | | 6043 | | |
| | | 6044 | | // For 0: |
| | | 6045 | | TypeParameter tp; |
| | | 6046 | | #if ASYNC_TASK_TYPES |
| | | 6047 | | // For 1: |
| | | 6048 | | Dictionary<string, MemberDecl> members; |
| | | 6049 | | // For 1 and 3: |
| | | 6050 | | MemberDecl member = null; |
| | | 6051 | | #endif |
| | | 6052 | | // For 2: |
| | | 6053 | | |
| | 111670 | 6054 | | tp = allTypeParameters.Find(expr.Name); |
| | 128470 | 6055 | | if (tp != null) { |
| | | 6056 | | // ----- 0. type parameter |
| | 33600 | 6057 | | if (expr.OptTypeArguments == null) { |
| | 16800 | 6058 | | r = new Resolver_IdentifierExpr(expr.tok, tp); |
| | 16800 | 6059 | | } else { |
| | 0 | 6060 | | reporter.Error(MessageSource.Resolver, expr.tok, "Type parameter expects no type arguments: {0}", expr.Name); |
| | 0 | 6061 | | } |
| | | 6062 | | #if ASYNC_TASK_TYPES // At the moment, there is no way for a class member to part of a type name, but this changes with |
| | | 6063 | | } else if (currentClass != null && classMembers.TryGetValue(currentClass, out members) && members.TryGetValue(expr |
| | | 6064 | | // ----- 1. member of the enclosing class |
| | | 6065 | | Expression receiver; |
| | | 6066 | | if (member.IsStatic) { |
| | | 6067 | | receiver = new StaticReceiverExpr(expr.tok, (ClassDecl)member.EnclosingClass); |
| | | 6068 | | } else { |
| | | 6069 | | if (!scope.AllowInstance) { |
| | | 6070 | | reporter.Error(MessageSource.Resolver, expr.tok, "'this' is not allowed in a 'static' context"); |
| | | 6071 | | // nevertheless, set "receiver" to a value so we can continue resolution |
| | | 6072 | | } |
| | | 6073 | | receiver = new ImplicitThisExpr(expr.tok); |
| | | 6074 | | receiver.Type = GetThisType(expr.tok, (ClassDecl)member.EnclosingClass); // resolve here |
| | | 6075 | | } |
| | | 6076 | | r = ResolveExprDotCall(expr.tok, receiver, member, expr.OptTypeArguments, opts.resolutionContext, allowMethodCal |
| | | 6077 | | #endif |
| | 206540 | 6078 | | } else if (moduleInfo.TopLevels.TryGetValue(expr.Name, out var decl)) { |
| | | 6079 | | // ----- 2. Member of the enclosing module |
| | 94870 | 6080 | | if (decl is AmbiguousTopLevelDecl) { |
| | 0 | 6081 | | var ad = (AmbiguousTopLevelDecl)decl; |
| | 0 | 6082 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a type in one of the modu |
| | 94870 | 6083 | | } else { |
| | | 6084 | | // We have found a module name or a type name, neither of which is a type expression. However, the NameSegment |
| | | 6085 | | // looking at may be followed by a further suffix that makes this into a type expresion. We postpone the rest |
| | | 6086 | | // resolution to any such suffix. For now, we create a temporary expression that will never be seen by the com |
| | | 6087 | | // or verifier, just to have a placeholder where we can recorded what we have found. |
| | 94870 | 6088 | | r = CreateResolver_IdentifierExpr(expr.tok, expr.Name, expr.OptTypeArguments, decl); |
| | 94870 | 6089 | | } |
| | | 6090 | | |
| | | 6091 | | #if ASYNC_TASK_TYPES // At the moment, there is no way for a class member to part of a type name, but this changes with |
| | | 6092 | | } else if (moduleInfo.StaticMembers.TryGetValue(expr.Name, out member)) { |
| | | 6093 | | // ----- 3. static member of the enclosing module |
| | | 6094 | | Contract.Assert(member.IsStatic); // moduleInfo.StaticMembers is supposed to contain only static members of the |
| | | 6095 | | if (ReallyAmbiguousThing(ref member)) { |
| | | 6096 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a static member in one of |
| | | 6097 | | } else { |
| | | 6098 | | var receiver = new StaticReceiverExpr(expr.tok, (ClassDecl)member.EnclosingClass); |
| | | 6099 | | r = ResolveExprDotCall(expr.tok, receiver, member, expr.OptTypeArguments, opts.resolutionContext, allowMethodC |
| | | 6100 | | } |
| | | 6101 | | #endif |
| | 94870 | 6102 | | } else { |
| | | 6103 | | // ----- None of the above |
| | 0 | 6104 | | reporter.Error(MessageSource.Resolver, expr.tok, "Type or type parameter is not declared in this scope: {0} (did |
| | 0 | 6105 | | } |
| | | 6106 | | |
| | 111670 | 6107 | | if (r == null) { |
| | | 6108 | | // an error has been reported above; we won't fill in .ResolvedExpression, but we still must fill in .Type |
| | 0 | 6109 | | expr.Type = new InferredTypeProxy(); |
| | 111670 | 6110 | | } else { |
| | 111670 | 6111 | | expr.ResolvedExpression = r; |
| | 111670 | 6112 | | expr.Type = r.Type; |
| | 111670 | 6113 | | } |
| | 111670 | 6114 | | } |
| | | 6115 | | |
| | | 6116 | | /// <summary> |
| | | 6117 | | /// To resolve "id" in expression "E . id", do: |
| | | 6118 | | /// * If E denotes a module name M: |
| | | 6119 | | /// 0. If isLastNameSegment: |
| | | 6120 | | /// Unambiguous constructor name of a datatype in module M (if two constructors have the same name, an error |
| | | 6121 | | /// (Language design note: If the constructor name is ambiguous or if one of the steps above takes priority |
| | | 6122 | | /// 1. Member of module M: sub-module (including submodules of imports), class, datatype, etc. |
| | | 6123 | | /// (if two imported types have the same name, an error message is produced here) |
| | | 6124 | | /// 2. Static function or method of M._default |
| | | 6125 | | /// (Note that in contrast to ResolveNameSegment, imported modules, etc. are ignored) |
| | | 6126 | | /// * If E denotes a type: |
| | | 6127 | | /// 3. Look up id as a member of that type |
| | | 6128 | | /// * If E denotes an expression: |
| | | 6129 | | /// 4. Let T be the type of E. Look up id in T. |
| | | 6130 | | /// </summary> |
| | | 6131 | | /// <param name="expr"></param> |
| | | 6132 | | /// <param name="isLastNameSegment">Indicates that the ExprDotName is not directly enclosed in another ExprDotName e |
| | | 6133 | | /// <param name="args">If the ExprDotName is enclosed in an ApplySuffix, then these are the arguments. The method r |
| | | 6134 | | /// that these arguments, if any, were not used. If args is non-null and the method does use them, the method retur |
| | | 6135 | | /// that incorporates these arguments.</param> |
| | | 6136 | | /// <param name="resolutionContext"></param> |
| | | 6137 | | /// <param name="allowMethodCall">If false, generates an error if the name denotes a method. If true and the name de |
| | | 6138 | | /// a Resolver_MethodCall.</param> |
| | 8050 | 6139 | | Expression ResolveDotSuffix(ExprDotName expr, bool isLastNameSegment, List<ActualBinding> args, ResolutionContext re |
| | | 6140 | | Contract.Requires(expr != null); |
| | | 6141 | | Contract.Requires(!expr.WasResolved()); |
| | | 6142 | | Contract.Requires(resolutionContext != null); |
| | | 6143 | | Contract.Ensures(Contract.Result<Expression>() == null || args != null); |
| | | 6144 | | |
| | | 6145 | | // resolve the LHS expression |
| | | 6146 | | // LHS should not be reveal lemma |
| | 8050 | 6147 | | ModuleDecl shadowedImport = null; |
| | 8050 | 6148 | | ResolutionContext nonRevealOpts = resolutionContext with { InReveal = false }; |
| | 12670 | 6149 | | if (expr.Lhs is NameSegment) { |
| | 4620 | 6150 | | ResolveNameSegment((NameSegment)expr.Lhs, false, null, nonRevealOpts, false, true, out shadowedImport); |
| | 9000 | 6151 | | } else if (expr.Lhs is ExprDotName) { |
| | 950 | 6152 | | ResolveDotSuffix((ExprDotName)expr.Lhs, false, null, nonRevealOpts, false); |
| | 3430 | 6153 | | } else { |
| | 2480 | 6154 | | ResolveExpression(expr.Lhs, nonRevealOpts); |
| | 2480 | 6155 | | } |
| | | 6156 | | |
| | 8050 | 6157 | | if (expr.OptTypeArguments != null) { |
| | 0 | 6158 | | foreach (var ty in expr.OptTypeArguments) { |
| | 0 | 6159 | | ResolveType(expr.tok, ty, resolutionContext, ResolveTypeOptionEnum.InferTypeProxies, null); |
| | 0 | 6160 | | } |
| | 0 | 6161 | | } |
| | | 6162 | | |
| | 8050 | 6163 | | Expression r = null; // the resolved expression, if successful |
| | 8050 | 6164 | | Expression rWithArgs = null; // the resolved expression after incorporating "args" |
| | 8050 | 6165 | | MemberDecl member = null; |
| | | 6166 | | |
| | 8050 | 6167 | | var name = resolutionContext.InReveal ? "reveal_" + expr.SuffixName : expr.SuffixName; |
| | 8050 | 6168 | | if (!expr.Lhs.WasResolved()) { |
| | 0 | 6169 | | return null; |
| | | 6170 | | } |
| | 8050 | 6171 | | var lhs = expr.Lhs.Resolved; |
| | 8050 | 6172 | | if (lhs != null && lhs.Type is Resolver_IdentifierExpr.ResolverType_Module) { |
| | 0 | 6173 | | var ri = (Resolver_IdentifierExpr)lhs; |
| | 0 | 6174 | | var sig = ((ModuleDecl)ri.Decl).AccessibleSignature(useCompileSignatures); |
| | 0 | 6175 | | sig = GetSignature(sig); |
| | | 6176 | | // For 0: |
| | | 6177 | | // For 1: |
| | | 6178 | | |
| | 0 | 6179 | | if (isLastNameSegment && sig.Ctors.TryGetValue(name, out var pair)) { |
| | | 6180 | | // ----- 0. datatype constructor |
| | 0 | 6181 | | if (pair.Item2) { |
| | | 6182 | | // there is more than one constructor with this name |
| | 0 | 6183 | | reporter.Error(MessageSource.Resolver, expr.tok, "the name '{0}' denotes a datatype constructor in module {2 |
| | 0 | 6184 | | } else { |
| | 0 | 6185 | | if (expr.OptTypeArguments != null) { |
| | 0 | 6186 | | reporter.Error(MessageSource.Resolver, expr.tok, "datatype constructor does not take any type parameters ( |
| | 0 | 6187 | | } |
| | 0 | 6188 | | var rr = new DatatypeValue(expr.tok, pair.Item1.EnclosingDatatype.Name, name, args ?? new List<ActualBinding |
| | 0 | 6189 | | ResolveDatatypeValue(resolutionContext, rr, pair.Item1.EnclosingDatatype, null); |
| | | 6190 | | |
| | 0 | 6191 | | if (args == null) { |
| | 0 | 6192 | | r = rr; |
| | 0 | 6193 | | } else { |
| | 0 | 6194 | | r = rr; // this doesn't really matter, since we're returning an "rWithArgs" (but if would have been prope |
| | 0 | 6195 | | rWithArgs = rr; |
| | 0 | 6196 | | } |
| | 0 | 6197 | | } |
| | 0 | 6198 | | } else if (sig.TopLevels.TryGetValue(name, out var decl)) { |
| | | 6199 | | // ----- 1. Member of the specified module |
| | 0 | 6200 | | if (decl is AmbiguousTopLevelDecl) { |
| | 0 | 6201 | | var ad = (AmbiguousTopLevelDecl)decl; |
| | 0 | 6202 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a type in one of the mo |
| | 0 | 6203 | | } else { |
| | | 6204 | | // We have found a module name or a type name, neither of which is an expression. However, the ExprDotName w |
| | | 6205 | | // looking at may be followed by a further suffix that makes this into an expresion. We postpone the rest of |
| | | 6206 | | // resolution to any such suffix. For now, we create a temporary expression that will never be seen by the c |
| | | 6207 | | // or verifier, just to have a placeholder where we can recorded what we have found. |
| | 0 | 6208 | | if (!isLastNameSegment) { |
| | 0 | 6209 | | if (decl is ClassDecl cd && cd.NonNullTypeDecl != null && name != cd.NonNullTypeDecl.Name) { |
| | | 6210 | | // A possibly-null type C? was mentioned. But it does not have any further members. The program should h |
| | | 6211 | | // the name of the class, C. Report an error and continue. |
| | 0 | 6212 | | reporter.Error(MessageSource.Resolver, expr.tok, "To access members of {0} '{1}', write '{1}', not '{2}' |
| | 0 | 6213 | | } |
| | 0 | 6214 | | } |
| | 0 | 6215 | | r = CreateResolver_IdentifierExpr(expr.tok, name, expr.OptTypeArguments, decl); |
| | 0 | 6216 | | } |
| | 0 | 6217 | | } else if (sig.StaticMembers.TryGetValue(name, out member)) { |
| | | 6218 | | // ----- 2. static member of the specified module |
| | 0 | 6219 | | Contract.Assert(member.IsStatic); // moduleInfo.StaticMembers is supposed to contain only static members of th |
| | 0 | 6220 | | if (member is AmbiguousMemberDecl) { |
| | 0 | 6221 | | var ambiguousMember = (AmbiguousMemberDecl)member; |
| | 0 | 6222 | | reporter.Error(MessageSource.Resolver, expr.tok, "The name {0} ambiguously refers to a static member in one |
| | 0 | 6223 | | } else { |
| | 0 | 6224 | | var receiver = new StaticReceiverExpr(expr.Lhs.tok, (ClassDecl)member.EnclosingClass, false); |
| | 0 | 6225 | | receiver.ContainerExpression = expr.Lhs; |
| | 0 | 6226 | | r = ResolveExprDotCall(expr.tok, receiver, null, member, args, expr.OptTypeArguments, resolutionContext, all |
| | 0 | 6227 | | } |
| | 0 | 6228 | | } else { |
| | 0 | 6229 | | reporter.Error(MessageSource.Resolver, expr.tok, "unresolved identifier: {0}", name); |
| | 0 | 6230 | | } |
| | | 6231 | | |
| | 8050 | 6232 | | } else if (lhs != null && lhs.Type is Resolver_IdentifierExpr.ResolverType_Type) { |
| | 0 | 6233 | | var ri = (Resolver_IdentifierExpr)lhs; |
| | | 6234 | | // ----- 3. Look up name in type |
| | | 6235 | | // expand any synonyms |
| | 0 | 6236 | | var ty = new UserDefinedType(expr.tok, ri.Decl.Name, ri.Decl, ri.TypeArgs).NormalizeExpand(); |
| | 0 | 6237 | | if (ty.IsDatatype) { |
| | | 6238 | | // ----- LHS is a datatype |
| | 0 | 6239 | | var dt = ty.AsDatatype; |
| | 0 | 6240 | | if (dt.ConstructorsByName != null && dt.ConstructorsByName.TryGetValue(name, out var ctor)) { |
| | 0 | 6241 | | if (expr.OptTypeArguments != null) { |
| | 0 | 6242 | | reporter.Error(MessageSource.Resolver, expr.tok, "datatype constructor does not take any type parameters ( |
| | 0 | 6243 | | } |
| | 0 | 6244 | | var rr = new DatatypeValue(expr.tok, ctor.EnclosingDatatype.Name, name, args ?? new List<ActualBinding>()); |
| | 0 | 6245 | | ResolveDatatypeValue(resolutionContext, rr, ctor.EnclosingDatatype, ty); |
| | 0 | 6246 | | if (args == null) { |
| | 0 | 6247 | | r = rr; |
| | 0 | 6248 | | } else { |
| | 0 | 6249 | | r = rr; // this doesn't really matter, since we're returning an "rWithArgs" (but if would have been prope |
| | 0 | 6250 | | rWithArgs = rr; |
| | 0 | 6251 | | } |
| | 0 | 6252 | | } |
| | 0 | 6253 | | } |
| | 0 | 6254 | | var cd = r == null ? ty.AsTopLevelTypeWithMembersBypassInternalSynonym : null; |
| | 0 | 6255 | | if (cd != null) { |
| | | 6256 | | // ----- LHS is a type with members |
| | 0 | 6257 | | if (classMembers.TryGetValue(cd, out var members) && members.TryGetValue(name, out member)) { |
| | 0 | 6258 | | if (!VisibleInScope(member)) { |
| | 0 | 6259 | | reporter.Error(MessageSource.Resolver, expr.tok, "member '{0}' has not been imported in this scope and can |
| | 0 | 6260 | | } |
| | 0 | 6261 | | if (!member.IsStatic) { |
| | 0 | 6262 | | reporter.Error(MessageSource.Resolver, expr.tok, "accessing member '{0}' requires an instance expression", |
| | | 6263 | | // nevertheless, continue creating an expression that approximates a correct one |
| | 0 | 6264 | | } |
| | 0 | 6265 | | var receiver = new StaticReceiverExpr(expr.Lhs.tok, (UserDefinedType)ty.NormalizeExpand(), (TopLevelDeclWith |
| | 0 | 6266 | | receiver.ContainerExpression = expr.Lhs; |
| | 0 | 6267 | | r = ResolveExprDotCall(expr.tok, receiver, null, member, args, expr.OptTypeArguments, resolutionContext, all |
| | 0 | 6268 | | } |
| | 0 | 6269 | | } |
| | 0 | 6270 | | if (r == null) { |
| | 0 | 6271 | | reporter.Error(MessageSource.Resolver, expr.tok, "member '{0}' does not exist in {2} '{1}'", name, ri.Decl.Nam |
| | 0 | 6272 | | } |
| | 16100 | 6273 | | } else if (lhs != null) { |
| | | 6274 | | // ----- 4. Look up name in the type of the Lhs |
| | 8050 | 6275 | | member = ResolveMember(expr.tok, expr.Lhs.Type, name, out var tentativeReceiverType); |
| | 16100 | 6276 | | if (member != null) { |
| | | 6277 | | Expression receiver; |
| | 16100 | 6278 | | if (!member.IsStatic) { |
| | 8050 | 6279 | | receiver = expr.Lhs; |
| | 8050 | 6280 | | AddAssignableConstraint(expr.tok, tentativeReceiverType, receiver.Type, "receiver type ({1}) does not have a |
| | 8050 | 6281 | | r = ResolveExprDotCall(expr.tok, receiver, tentativeReceiverType, member, args, expr.OptTypeArguments, resol |
| | 8050 | 6282 | | } else { |
| | 0 | 6283 | | receiver = new StaticReceiverExpr(expr.tok, (UserDefinedType)tentativeReceiverType, (TopLevelDeclWithMembers |
| | 0 | 6284 | | r = ResolveExprDotCall(expr.tok, receiver, null, member, args, expr.OptTypeArguments, resolutionContext, all |
| | 0 | 6285 | | } |
| | 8050 | 6286 | | } |
| | 8050 | 6287 | | } |
| | | 6288 | | |
| | 8050 | 6289 | | if (r == null) { |
| | | 6290 | | // an error has been reported above; we won't fill in .ResolvedExpression, but we still must fill in .Type |
| | 0 | 6291 | | expr.Type = new InferredTypeProxy(); |
| | 8050 | 6292 | | } else { |
| | 8050 | 6293 | | CheckForAmbiguityInShadowedImportedModule(shadowedImport, name, expr.tok, useCompileSignatures, isLastNameSegmen |
| | 8050 | 6294 | | expr.ResolvedExpression = r; |
| | 8050 | 6295 | | expr.Type = r.Type; |
| | 8050 | 6296 | | } |
| | 8050 | 6297 | | return rWithArgs; |
| | 8050 | 6298 | | } |
| | | 6299 | | |
| | | 6300 | | /// <summary> |
| | | 6301 | | /// Check whether the name we just resolved may have been resolved differently if we didn't allow member `M.M` of |
| | | 6302 | | /// module `M` to shadow `M` when the user writes `import opened M`. Raising an error in that case allowed us to |
| | | 6303 | | /// change the behavior of `import opened` without silently changing the meaning of existing programs. |
| | | 6304 | | /// (https://github.com/dafny-lang/dafny/issues/1996) |
| | | 6305 | | /// |
| | | 6306 | | /// Note the extra care for the constructor case, which is needed because the constructors of datatype `M.M` are |
| | | 6307 | | /// exposed through both `M` and `M.M`, without ambiguity. |
| | | 6308 | | /// </summary> |
| | | 6309 | | private void CheckForAmbiguityInShadowedImportedModule(ModuleDecl moduleDecl, string name, |
| | 8050 | 6310 | | IToken tok, bool useCompileSignatures, bool isLastNameSegment) { |
| | 8050 | 6311 | | if (moduleDecl != null && NameConflictsWithModuleContents(moduleDecl, name, useCompileSignatures, isLastNameSegmen |
| | 0 | 6312 | | reporter.Error(MessageSource.Resolver, tok, |
| | 0 | 6313 | | "Reference to member '{0}' is ambiguous: name '{1}' shadows an import-opened module of the same name, and " |
| | 0 | 6314 | | + "both have a member '{0}'. To solve this issue, give a different name to the imported module using " |
| | 0 | 6315 | | + "`import opened XYZ = ...` instead of `import opened ...`.", |
| | 0 | 6316 | | name, moduleDecl.Name); |
| | 0 | 6317 | | } |
| | 8050 | 6318 | | } |
| | | 6319 | | |
| | 0 | 6320 | | private bool NameConflictsWithModuleContents(ModuleDecl moduleDecl, string name, bool useCompileSignatures, bool isL |
| | 0 | 6321 | | var sig = GetSignature(moduleDecl.AccessibleSignature(useCompileSignatures)); |
| | 0 | 6322 | | return ( |
| | 0 | 6323 | | (isLastNameSegment |
| | 0 | 6324 | | && sig.Ctors.GetValueOrDefault(name) is { Item1: var constructor, Item2: var ambiguous } |
| | 0 | 6325 | | && !ambiguous && constructor.EnclosingDatatype.Name != moduleDecl.Name) |
| | 0 | 6326 | | || sig.TopLevels.ContainsKey(name) |
| | 0 | 6327 | | || sig.StaticMembers.ContainsKey(name) |
| | 0 | 6328 | | ); |
| | 0 | 6329 | | } |
| | | 6330 | | |
| | | 6331 | | Expression ResolveExprDotCall(IToken tok, Expression receiver, Type receiverTypeBound/*?*/, |
| | 27140 | 6332 | | MemberDecl member, List<ActualBinding> args, List<Type> optTypeArguments, ResolutionContext resolutionContext, boo |
| | | 6333 | | Contract.Requires(tok != null); |
| | | 6334 | | Contract.Requires(receiver != null); |
| | | 6335 | | Contract.Requires(receiver.WasResolved()); |
| | | 6336 | | Contract.Requires(member != null); |
| | | 6337 | | Contract.Requires(resolutionContext != null && resolutionContext.CodeContext != null); |
| | | 6338 | | |
| | 27140 | 6339 | | var rr = new MemberSelectExpr(tok, receiver, member.Name); |
| | 27140 | 6340 | | rr.Member = member; |
| | | 6341 | | |
| | | 6342 | | // Now, fill in rr.Type. This requires taking into consideration the type parameters passed to the receiver's typ |
| | | 6343 | | // parameters used in this NameSegment/ExprDotName. |
| | | 6344 | | // Add to "subst" the type parameters given to the member's class/datatype |
| | 27140 | 6345 | | rr.TypeApplication_AtEnclosingClass = new List<Type>(); |
| | 27140 | 6346 | | rr.TypeApplication_JustMember = new List<Type>(); |
| | | 6347 | | Dictionary<TypeParameter, Type> subst; |
| | 27140 | 6348 | | var rType = (receiverTypeBound ?? receiver.Type).NormalizeExpand(); |
| | 54280 | 6349 | | if (rType is UserDefinedType udt && udt.ResolvedClass != null) { |
| | 27140 | 6350 | | subst = TypeParameter.SubstitutionMap(udt.ResolvedClass.TypeArgs, udt.TypeArgs); |
| | 27140 | 6351 | | if (member.EnclosingClass == null) { |
| | | 6352 | | // this can happen for some special members, like real.Floor |
| | 27140 | 6353 | | } else { |
| | 27140 | 6354 | | rr.TypeApplication_AtEnclosingClass.AddRange(rType.AsParentType(member.EnclosingClass).TypeArgs); |
| | 27140 | 6355 | | } |
| | 27140 | 6356 | | } else { |
| | 0 | 6357 | | var vtd = AsValuetypeDecl(rType); |
| | 0 | 6358 | | if (vtd != null) { |
| | 0 | 6359 | | Contract.Assert(vtd.TypeArgs.Count == rType.TypeArgs.Count); |
| | 0 | 6360 | | subst = TypeParameter.SubstitutionMap(vtd.TypeArgs, rType.TypeArgs); |
| | 0 | 6361 | | rr.TypeApplication_AtEnclosingClass.AddRange(rType.TypeArgs); |
| | 0 | 6362 | | } else { |
| | 0 | 6363 | | Contract.Assert(rType.TypeArgs.Count == 0); |
| | 0 | 6364 | | subst = new Dictionary<TypeParameter, Type>(); |
| | 0 | 6365 | | } |
| | 0 | 6366 | | } |
| | | 6367 | | |
| | 35190 | 6368 | | if (member is Field) { |
| | 8050 | 6369 | | var field = (Field)member; |
| | 8050 | 6370 | | if (optTypeArguments != null) { |
| | 0 | 6371 | | reporter.Error(MessageSource.Resolver, tok, "a field ({0}) does not take any type arguments (got {1})", field. |
| | 0 | 6372 | | } |
| | 8050 | 6373 | | subst = BuildTypeArgumentSubstitute(subst, receiverTypeBound ?? receiver.Type); |
| | 8050 | 6374 | | rr.Type = field.Type.Subst(subst); |
| | 36110 | 6375 | | } else if (member is Function) { |
| | 8970 | 6376 | | var fn = (Function)member; |
| | 8970 | 6377 | | if (fn is TwoStateFunction && !resolutionContext.IsTwoState) { |
| | 0 | 6378 | | reporter.Error(MessageSource.Resolver, tok, "two-state function ('{0}') can only be called in a two-state cont |
| | 0 | 6379 | | } |
| | 8970 | 6380 | | int suppliedTypeArguments = optTypeArguments == null ? 0 : optTypeArguments.Count; |
| | 8970 | 6381 | | if (optTypeArguments != null && suppliedTypeArguments != fn.TypeArgs.Count) { |
| | 0 | 6382 | | reporter.Error(MessageSource.Resolver, tok, "function '{0}' expects {1} type argument{2} (got {3})", |
| | 0 | 6383 | | member.Name, fn.TypeArgs.Count, Util.Plural(fn.TypeArgs.Count), suppliedTypeArguments); |
| | 0 | 6384 | | } |
| | 31920 | 6385 | | for (int i = 0; i < fn.TypeArgs.Count; i++) { |
| | 4660 | 6386 | | var ta = i < suppliedTypeArguments ? optTypeArguments[i] : new InferredTypeProxy(); |
| | 4660 | 6387 | | rr.TypeApplication_JustMember.Add(ta); |
| | 4660 | 6388 | | subst.Add(fn.TypeArgs[i], ta); |
| | 4660 | 6389 | | } |
| | 8970 | 6390 | | subst = BuildTypeArgumentSubstitute(subst, receiverTypeBound ?? receiver.Type); |
| | 8970 | 6391 | | rr.Type = SelectAppropriateArrowTypeForFunction(fn, subst, builtIns); |
| | 19090 | 6392 | | } else { |
| | | 6393 | | // the member is a method |
| | 10120 | 6394 | | var m = (Method)member; |
| | 10120 | 6395 | | if (!allowMethodCall) { |
| | | 6396 | | // it's a method and method calls are not allowed in the given context |
| | 0 | 6397 | | reporter.Error(MessageSource.Resolver, tok, "expression is not allowed to invoke a {0} ({1})", member.WhatKind |
| | 0 | 6398 | | } |
| | 10120 | 6399 | | int suppliedTypeArguments = optTypeArguments == null ? 0 : optTypeArguments.Count; |
| | 10120 | 6400 | | if (optTypeArguments != null && suppliedTypeArguments != m.TypeArgs.Count) { |
| | 0 | 6401 | | reporter.Error(MessageSource.Resolver, tok, "method '{0}' expects {1} type argument{2} (got {3})", |
| | 0 | 6402 | | member.Name, m.TypeArgs.Count, Util.Plural(m.TypeArgs.Count), suppliedTypeArguments); |
| | 0 | 6403 | | } |
| | 20240 | 6404 | | for (int i = 0; i < m.TypeArgs.Count; i++) { |
| | 0 | 6405 | | var ta = i < suppliedTypeArguments ? optTypeArguments[i] : new InferredTypeProxy(); |
| | 0 | 6406 | | rr.TypeApplication_JustMember.Add(ta); |
| | 0 | 6407 | | subst.Add(m.TypeArgs[i], ta); |
| | 0 | 6408 | | } |
| | 10120 | 6409 | | subst = BuildTypeArgumentSubstitute(subst, receiverTypeBound ?? receiver.Type); |
| | 25150 | 6410 | | rr.ResolvedOutparameterTypes = m.Outs.ConvertAll(f => f.Type.Subst(subst)); |
| | 10120 | 6411 | | rr.Type = new InferredTypeProxy(); // fill in this field, in order to make "rr" resolved |
| | 10120 | 6412 | | } |
| | 27140 | 6413 | | return rr; |
| | 27140 | 6414 | | } |
| | | 6415 | | |
| | 17660 | 6416 | | public MethodCallInformation ResolveApplySuffix(ApplySuffix e, ResolutionContext resolutionContext, bool allowMethod |
| | | 6417 | | Contract.Requires(e != null); |
| | | 6418 | | Contract.Requires(resolutionContext != null); |
| | | 6419 | | Contract.Ensures(Contract.Result<MethodCallInformation>() == null || allowMethodCall); |
| | 17660 | 6420 | | Expression r = null; // upon success, the expression to which the ApplySuffix resolves |
| | 17660 | 6421 | | var errorCount = reporter.Count(ErrorLevel.Error); |
| | 34800 | 6422 | | if (e.Lhs is NameSegment) { |
| | 17140 | 6423 | | r = ResolveNameSegment((NameSegment)e.Lhs, true, e.Bindings.ArgumentBindings, resolutionContext, allowMethodCall |
| | | 6424 | | // note, if r is non-null, then e.Args have been resolved and r is a resolved expression that incorporates e.Arg |
| | 17910 | 6425 | | } else if (e.Lhs is ExprDotName) { |
| | 250 | 6426 | | r = ResolveDotSuffix((ExprDotName)e.Lhs, true, e.Bindings.ArgumentBindings, resolutionContext, allowMethodCall); |
| | | 6427 | | // note, if r is non-null, then e.Args have been resolved and r is a resolved expression that incorporates e.Arg |
| | 520 | 6428 | | } else { |
| | 270 | 6429 | | ResolveExpression(e.Lhs, resolutionContext); |
| | 270 | 6430 | | } |
| | 17660 | 6431 | | if (e.Lhs.Type == null) { |
| | | 6432 | | // some error had been detected during the attempted resolution of e.Lhs |
| | 0 | 6433 | | e.Lhs.Type = new InferredTypeProxy(); |
| | 0 | 6434 | | } |
| | 17660 | 6435 | | Label atLabel = null; |
| | 17660 | 6436 | | if (e.AtTok != null) { |
| | 0 | 6437 | | atLabel = DominatingStatementLabels.Find(e.AtTok.val); |
| | 0 | 6438 | | if (atLabel == null) { |
| | 0 | 6439 | | reporter.Error(MessageSource.Resolver, e.AtTok, "no label '{0}' in scope at this time", e.AtTok.val); |
| | 0 | 6440 | | } |
| | 0 | 6441 | | } |
| | 35320 | 6442 | | if (r == null) { |
| | 17660 | 6443 | | var improvedType = PartiallyResolveTypeForMemberSelection(e.Lhs.tok, e.Lhs.Type, "_#apply"); |
| | 17660 | 6444 | | var fnType = improvedType.AsArrowType; |
| | 27780 | 6445 | | if (fnType == null) { |
| | 10120 | 6446 | | var lhs = e.Lhs.Resolved; |
| | 10120 | 6447 | | if (lhs != null && lhs.Type is Resolver_IdentifierExpr.ResolverType_Module) { |
| | 0 | 6448 | | reporter.Error(MessageSource.Resolver, e.tok, "name of module ({0}) is used as a function", ((Resolver_Ident |
| | 10120 | 6449 | | } else if (lhs != null && lhs.Type is Resolver_IdentifierExpr.ResolverType_Type) { |
| | 0 | 6450 | | var ri = (Resolver_IdentifierExpr)lhs; |
| | 0 | 6451 | | reporter.Error(MessageSource.Resolver, e.tok, "name of {0} ({1}) is used as a function", ri.Decl.WhatKind, r |
| | 10120 | 6452 | | } else { |
| | 20240 | 6453 | | if (lhs is MemberSelectExpr mse && mse.Member is Method) { |
| | 10120 | 6454 | | if (atLabel != null) { |
| | 0 | 6455 | | Contract.Assert(mse != null); // assured by the parser |
| | 0 | 6456 | | if (mse.Member is TwoStateLemma) { |
| | 0 | 6457 | | mse.AtLabel = atLabel; |
| | 0 | 6458 | | } else { |
| | 0 | 6459 | | reporter.Error(MessageSource.Resolver, e.AtTok, "an @-label can only be applied to a two-state lemma") |
| | 0 | 6460 | | } |
| | 0 | 6461 | | } |
| | 20240 | 6462 | | if (allowMethodCall) { |
| | 10120 | 6463 | | Contract.Assert(!e.Bindings.WasResolved); // we expect that .Bindings has not yet been processed, so we |
| | 10120 | 6464 | | var tok = Options.Get(DafnyConsolePrinter.ShowSnippets) ? e.RangeToken.ToToken() : e.tok; |
| | 10120 | 6465 | | var cRhs = new MethodCallInformation(tok, mse, e.Bindings.ArgumentBindings); |
| | 10120 | 6466 | | return cRhs; |
| | 0 | 6467 | | } else { |
| | 0 | 6468 | | reporter.Error(MessageSource.Resolver, e.tok, "{0} call is not allowed to be used in an expression conte |
| | 0 | 6469 | | } |
| | 0 | 6470 | | } else if (lhs != null) { // if e.Lhs.Resolved is null, then e.Lhs was not successfully resolved and an err |
| | 0 | 6471 | | reporter.Error(MessageSource.Resolver, e.tok, "non-function expression (of type {0}) is called with parame |
| | 0 | 6472 | | } |
| | 0 | 6473 | | } |
| | | 6474 | | // resolve the arguments, even in the presence of the errors above |
| | 0 | 6475 | | foreach (var binding in e.Bindings.ArgumentBindings) { |
| | 0 | 6476 | | ResolveExpression(binding.Actual, resolutionContext); |
| | 0 | 6477 | | } |
| | 7540 | 6478 | | } else { |
| | 7540 | 6479 | | var mse = e.Lhs is NameSegment || e.Lhs is ExprDotName ? e.Lhs.Resolved as MemberSelectExpr : null; |
| | 7540 | 6480 | | var callee = mse == null ? null : mse.Member as Function; |
| | 7540 | 6481 | | if (atLabel != null && !(callee is TwoStateFunction)) { |
| | 0 | 6482 | | reporter.Error(MessageSource.Resolver, e.AtTok, "an @-label can only be applied to a two-state function"); |
| | 0 | 6483 | | atLabel = null; |
| | 0 | 6484 | | } |
| | 14560 | 6485 | | if (callee != null) { |
| | | 6486 | | // produce a FunctionCallExpr instead of an ApplyExpr(MemberSelectExpr) |
| | 7020 | 6487 | | var rr = new FunctionCallExpr(e.Lhs.tok, callee.Name, mse.Obj, e.tok, e.CloseParen, e.Bindings, atLabel) { |
| | 7020 | 6488 | | Function = callee, |
| | 7020 | 6489 | | TypeApplication_AtEnclosingClass = mse.TypeApplication_AtEnclosingClass, |
| | 7020 | 6490 | | TypeApplication_JustFunction = mse.TypeApplication_JustMember |
| | 7020 | 6491 | | }; |
| | 7020 | 6492 | | var typeMap = BuildTypeArgumentSubstitute(mse.TypeArgumentSubstitutionsAtMemberDeclaration()); |
| | 7020 | 6493 | | ResolveActualParameters(rr.Bindings, callee.Formals, e.tok, callee, resolutionContext, typeMap, callee.IsSta |
| | 7020 | 6494 | | rr.Type = callee.ResultType.Subst(typeMap); |
| | 14040 | 6495 | | if (errorCount == reporter.Count(ErrorLevel.Error)) { |
| | 7020 | 6496 | | Contract.Assert(!(mse.Obj is StaticReceiverExpr) || callee.IsStatic); // this should have been checked al |
| | 7020 | 6497 | | Contract.Assert(callee.Formals.Count == rr.Args.Count); // this should have been checked already |
| | 7020 | 6498 | | } |
| | 7020 | 6499 | | r = rr; |
| | 7540 | 6500 | | } else { |
| | | 6501 | | List<Formal> formals; |
| | 520 | 6502 | | if (callee != null) { |
| | 0 | 6503 | | formals = callee.Formals; |
| | 520 | 6504 | | } else { |
| | 520 | 6505 | | formals = new List<Formal>(); |
| | 5060 | 6506 | | for (var i = 0; i < fnType.Args.Count; i++) { |
| | 1340 | 6507 | | var argType = fnType.Args[i]; |
| | 1340 | 6508 | | var formal = new ImplicitFormal(e.tok, "_#p" + i, argType, true, false); |
| | 1340 | 6509 | | formals.Add(formal); |
| | 1340 | 6510 | | } |
| | 520 | 6511 | | } |
| | 520 | 6512 | | ResolveActualParameters(e.Bindings, formals, e.tok, fnType, resolutionContext, new Dictionary<TypeParameter, |
| | 520 | 6513 | | r = new ApplyExpr(e.Lhs.tok, e.Lhs, e.Args, e.CloseParen); |
| | 520 | 6514 | | r.Type = fnType.Result; |
| | 520 | 6515 | | } |
| | 7540 | 6516 | | } |
| | 7540 | 6517 | | } |
| | 7540 | 6518 | | if (r == null) { |
| | | 6519 | | // an error has been reported above; we won't fill in .ResolvedExpression, but we still must fill in .Type |
| | 0 | 6520 | | e.Type = new InferredTypeProxy(); |
| | 7540 | 6521 | | } else { |
| | 7540 | 6522 | | e.ResolvedExpression = r; |
| | 7540 | 6523 | | e.Type = r.Type; |
| | 7540 | 6524 | | } |
| | 7540 | 6525 | | return null; |
| | 17660 | 6526 | | } |
| | | 6527 | | |
| | | 6528 | | /// <summary> |
| | | 6529 | | /// the return value is false iff there is an error in resolving the datatype value; |
| | | 6530 | | /// if there is an error then an error message is emitted iff complain is true |
| | | 6531 | | /// </summary> |
| | 143140 | 6532 | | private bool ResolveDatatypeValue(ResolutionContext resolutionContext, DatatypeValue dtv, DatatypeDecl dt, Type ty, |
| | | 6533 | | Contract.Requires(resolutionContext != null); |
| | | 6534 | | Contract.Requires(dtv != null); |
| | | 6535 | | Contract.Requires(dt != null); |
| | | 6536 | | Contract.Requires(ty == null || (ty.AsDatatype == dt && ty.TypeArgs.Count == dt.TypeArgs.Count)); |
| | | 6537 | | |
| | 143140 | 6538 | | var ok = true; |
| | 143140 | 6539 | | var gt = new List<Type>(dt.TypeArgs.Count); |
| | 143140 | 6540 | | var subst = new Dictionary<TypeParameter, Type>(); |
| | 1077920 | 6541 | | for (int i = 0; i < dt.TypeArgs.Count; i++) { |
| | 263880 | 6542 | | Type t = ty == null ? new InferredTypeProxy() : ty.TypeArgs[i]; |
| | 263880 | 6543 | | gt.Add(t); |
| | 263880 | 6544 | | dtv.InferredTypeArgs.Add(t); |
| | 263880 | 6545 | | subst.Add(dt.TypeArgs[i], t); |
| | 263880 | 6546 | | } |
| | | 6547 | | // Construct a resolved type directly, as we know the declaration is dt. |
| | 143140 | 6548 | | dtv.Type = new UserDefinedType(dtv.tok, dt.Name, dt, gt); |
| | | 6549 | | |
| | 143140 | 6550 | | if (!dt.ConstructorsByName.TryGetValue(dtv.MemberName, out var ctor)) { |
| | 0 | 6551 | | ok = false; |
| | 0 | 6552 | | if (complain) { |
| | 0 | 6553 | | reporter.Error(MessageSource.Resolver, dtv.tok, "undeclared constructor {0} in datatype {1}", dtv.MemberName, |
| | 0 | 6554 | | } |
| | 143140 | 6555 | | } else { |
| | 143140 | 6556 | | Contract.Assert(ctor != null); // follows from postcondition of TryGetValue |
| | 143140 | 6557 | | dtv.Ctor = ctor; |
| | 143140 | 6558 | | } |
| | 286280 | 6559 | | if (complain && ctor != null) { |
| | 143140 | 6560 | | ResolveActualParameters(dtv.Bindings, ctor.Formals, dtv.tok, ctor, resolutionContext, subst, null); |
| | 143140 | 6561 | | } else { |
| | | 6562 | | // still resolve the expressions |
| | 0 | 6563 | | foreach (var binding in dtv.Bindings.ArgumentBindings) { |
| | 0 | 6564 | | ResolveExpression(binding.Actual, resolutionContext); |
| | 0 | 6565 | | } |
| | 0 | 6566 | | dtv.Bindings.AcceptArgumentExpressionsAsExactParameterList(); |
| | 0 | 6567 | | } |
| | | 6568 | | |
| | 143140 | 6569 | | return ok && ctor.Formals.Count == dtv.Arguments.Count; |
| | 143140 | 6570 | | } |
| | | 6571 | | |
| | 0 | 6572 | | public void ResolveFunctionCallExpr(FunctionCallExpr e, ResolutionContext resolutionContext) { |
| | | 6573 | | Contract.Requires(e != null); |
| | | 6574 | | Contract.Requires(e.Type == null); // should not have been type checked before |
| | | 6575 | | |
| | 0 | 6576 | | ResolveReceiver(e.Receiver, resolutionContext); |
| | 0 | 6577 | | Contract.Assert(e.Receiver.Type != null); // follows from postcondition of ResolveExpression |
| | | 6578 | | |
| | 0 | 6579 | | var member = ResolveMember(e.tok, e.Receiver.Type, e.Name, out var tentativeReceiverType); |
| | | 6580 | | #if !NO_WORK_TO_BE_DONE |
| | 0 | 6581 | | var ctype = (UserDefinedType)tentativeReceiverType; |
| | | 6582 | | #endif |
| | 0 | 6583 | | if (member == null) { |
| | | 6584 | | // error has already been reported by ResolveMember |
| | 0 | 6585 | | } else if (member is Method) { |
| | 0 | 6586 | | reporter.Error(MessageSource.Resolver, e, "member {0} in type {1} refers to a method, but only functions can be |
| | 0 | 6587 | | } else if (!(member is Function)) { |
| | 0 | 6588 | | reporter.Error(MessageSource.Resolver, e, "member {0} in type {1} does not refer to a function", e.Name, cce.Non |
| | 0 | 6589 | | } else { |
| | 0 | 6590 | | Function function = (Function)member; |
| | 0 | 6591 | | e.Function = function; |
| | 0 | 6592 | | if (function is TwoStateFunction && !resolutionContext.IsTwoState) { |
| | 0 | 6593 | | reporter.Error(MessageSource.Resolver, e.tok, "a two-state function can be used only in a two-state context"); |
| | 0 | 6594 | | } |
| | 0 | 6595 | | if (e.Receiver is StaticReceiverExpr && !function.IsStatic) { |
| | 0 | 6596 | | reporter.Error(MessageSource.Resolver, e, "an instance function must be selected via an object, not just a cla |
| | 0 | 6597 | | } |
| | 0 | 6598 | | Contract.Assert(ctype != null); // follows from postcondition of ResolveMember |
| | 0 | 6599 | | if (!function.IsStatic) { |
| | 0 | 6600 | | if (!scope.AllowInstance && e.Receiver is ThisExpr) { |
| | | 6601 | | // The call really needs an instance, but that instance is given as 'this', which is not |
| | | 6602 | | // available in this context. In most cases, occurrences of 'this' inside e.Receiver would |
| | | 6603 | | // have been caught in the recursive call to resolve e.Receiver, but not the specific case |
| | | 6604 | | // of e.Receiver being 'this' (explicitly or implicitly), for that case needs to be allowed |
| | | 6605 | | // in the event that a static function calls another static function (and note that we need the |
| | | 6606 | | // type of the receiver in order to find the method, so we could not have made this check |
| | | 6607 | | // earlier). |
| | 0 | 6608 | | reporter.Error(MessageSource.Resolver, e.Receiver, "'this' is not allowed in a 'static' context"); |
| | 0 | 6609 | | } else if (e.Receiver is StaticReceiverExpr) { |
| | 0 | 6610 | | reporter.Error(MessageSource.Resolver, e.Receiver, "call to instance function requires an instance"); |
| | 0 | 6611 | | } |
| | 0 | 6612 | | } |
| | | 6613 | | // build the type substitution map |
| | 0 | 6614 | | var typeMap = new Dictionary<TypeParameter, Type>(); |
| | 0 | 6615 | | for (int i = 0; i < ctype.TypeArgs.Count; i++) { |
| | 0 | 6616 | | typeMap.Add(ctype.ResolvedClass.TypeArgs[i], ctype.TypeArgs[i]); |
| | 0 | 6617 | | } |
| | 0 | 6618 | | var typeThatEnclosesMember = ctype.AsParentType(member.EnclosingClass); |
| | 0 | 6619 | | e.TypeApplication_AtEnclosingClass = new List<Type>(); |
| | 0 | 6620 | | for (int i = 0; i < typeThatEnclosesMember.TypeArgs.Count; i++) { |
| | 0 | 6621 | | e.TypeApplication_AtEnclosingClass.Add(typeThatEnclosesMember.TypeArgs[i]); |
| | 0 | 6622 | | } |
| | 0 | 6623 | | e.TypeApplication_JustFunction = new List<Type>(); |
| | 0 | 6624 | | foreach (TypeParameter p in function.TypeArgs) { |
| | 0 | 6625 | | var ty = new ParamTypeProxy(p); |
| | 0 | 6626 | | typeMap.Add(p, ty); |
| | 0 | 6627 | | e.TypeApplication_JustFunction.Add(ty); |
| | 0 | 6628 | | } |
| | 0 | 6629 | | Dictionary<TypeParameter, Type> subst = BuildTypeArgumentSubstitute(typeMap); |
| | | 6630 | | |
| | | 6631 | | // type check the arguments |
| | 0 | 6632 | | ResolveActualParameters(e.Bindings, function.Formals, e.tok, function, resolutionContext, subst, function.IsStat |
| | | 6633 | | |
| | 0 | 6634 | | e.Type = function.ResultType.Subst(subst).NormalizeExpand(); |
| | 0 | 6635 | | } |
| | 0 | 6636 | | } |
| | | 6637 | | |
| | 0 | 6638 | | void ResolveReceiver(Expression expr, ResolutionContext resolutionContext) { |
| | | 6639 | | Contract.Requires(expr != null); |
| | | 6640 | | Contract.Ensures(expr.Type != null); |
| | | 6641 | | |
| | 0 | 6642 | | if (expr is ThisExpr && !expr.WasResolved()) { |
| | | 6643 | | // Allow 'this' here, regardless of scope.AllowInstance. The caller is responsible for |
| | | 6644 | | // making sure 'this' does not really get used when it's not available. |
| | 0 | 6645 | | Contract.Assume(currentClass != null); // this is really a precondition, in this case |
| | 0 | 6646 | | expr.Type = GetThisType(expr.tok, currentClass); |
| | 0 | 6647 | | } else { |
| | 0 | 6648 | | ResolveExpression(expr, resolutionContext); |
| | 0 | 6649 | | } |
| | 0 | 6650 | | } |
| | | 6651 | | |
| | 13030 | 6652 | | void ResolveSeqSelectExpr(SeqSelectExpr e, ResolutionContext resolutionContext) { |
| | | 6653 | | Contract.Requires(e != null); |
| | 13030 | 6654 | | if (e.Type != null) { |
| | | 6655 | | // already resolved |
| | 0 | 6656 | | return; |
| | | 6657 | | } |
| | | 6658 | | |
| | 13030 | 6659 | | ResolveExpression(e.Seq, resolutionContext); |
| | 13030 | 6660 | | Contract.Assert(e.Seq.Type != null); // follows from postcondition of ResolveExpression |
| | | 6661 | | |
| | 14300 | 6662 | | if (e.SelectOne) { |
| | 1270 | 6663 | | AddXConstraint(e.tok, "Indexable", e.Seq.Type, "element selection requires a sequence, array, multiset, or map ( |
| | 1270 | 6664 | | ResolveExpression(e.E0, resolutionContext); |
| | 1270 | 6665 | | AddXConstraint(e.E0.tok, "ContainerIndex", e.Seq.Type, e.E0.Type, "incorrect type for selection into {0} (got {1 |
| | 1270 | 6666 | | Contract.Assert(e.E1 == null); |
| | 1270 | 6667 | | e.Type = new InferredTypeProxy() { KeepConstraints = true }; |
| | 1270 | 6668 | | AddXConstraint(e.tok, "ContainerResult", |
| | 1270 | 6669 | | e.Seq.Type, e.Type, |
| | 1270 | 6670 | | new SeqSelectOneErrorMsg(e.tok, e.Seq.Type, e.Type)); |
| | 13030 | 6671 | | } else { |
| | 11760 | 6672 | | AddXConstraint(e.tok, "MultiIndexable", e.Seq.Type, "multi-selection of elements requires a sequence or array (g |
| | 19320 | 6673 | | if (e.E0 != null) { |
| | 7560 | 6674 | | ResolveExpression(e.E0, resolutionContext); |
| | 7560 | 6675 | | AddXConstraint(e.E0.tok, "ContainerIndex", e.Seq.Type, e.E0.Type, "incorrect type for selection into {0} (got |
| | 7560 | 6676 | | ConstrainSubtypeRelation(NewIntegerBasedProxy(e.tok), e.E0.Type, e.E0, "wrong number of indices for multi-sele |
| | 7560 | 6677 | | } |
| | 21000 | 6678 | | if (e.E1 != null) { |
| | 9240 | 6679 | | ResolveExpression(e.E1, resolutionContext); |
| | 9240 | 6680 | | AddXConstraint(e.E1.tok, "ContainerIndex", e.Seq.Type, e.E1.Type, "incorrect type for selection into {0} (got |
| | 9240 | 6681 | | ConstrainSubtypeRelation(NewIntegerBasedProxy(e.tok), e.E1.Type, e.E1, "wrong number of indices for multi-sele |
| | 9240 | 6682 | | } |
| | 11760 | 6683 | | var resultType = new InferredTypeProxy() { KeepConstraints = true }; |
| | 11760 | 6684 | | e.Type = new SeqType(resultType); |
| | 11760 | 6685 | | AddXConstraint(e.tok, "ContainerResult", e.Seq.Type, resultType, "multi-selection has type {0} which is incompat |
| | 11760 | 6686 | | } |
| | 13030 | 6687 | | } |
| | | 6688 | | |
| | | 6689 | | } |
| | | 6690 | | |
| | | 6691 | | public class MethodCallInformation { |
| | | 6692 | | public readonly IToken Tok; |
| | | 6693 | | public readonly MemberSelectExpr Callee; |
| | | 6694 | | public readonly List<ActualBinding> ActualParameters; |
| | | 6695 | | |
| | | 6696 | | [ContractInvariantMethod] |
| | | 6697 | | void ObjectInvariant() { |
| | | 6698 | | Contract.Invariant(Tok != null); |
| | | 6699 | | Contract.Invariant(Callee != null); |
| | | 6700 | | Contract.Invariant(Callee.Member is Method); |
| | | 6701 | | Contract.Invariant(ActualParameters != null); |
| | | 6702 | | } |
| | | 6703 | | |
| | | 6704 | | public MethodCallInformation(IToken tok, MemberSelectExpr callee, List<ActualBinding> actualParameters) { |
| | | 6705 | | Contract.Requires(tok != null); |
| | | 6706 | | Contract.Requires(callee != null); |
| | | 6707 | | Contract.Requires(callee.Member is Method); |
| | | 6708 | | Contract.Requires(actualParameters != null); |
| | | 6709 | | this.Tok = tok; |
| | | 6710 | | this.Callee = callee; |
| | | 6711 | | this.ActualParameters = actualParameters; |
| | | 6712 | | } |
| | | 6713 | | } |
| | | 6714 | | } |